Php 1 variable works for me, but the other 3 do not

-1

I have 3 variables to save an error message, the first one works for me, but I added 3 more and they get the error message: Undefined variable:

    <?php
if( isset($_GET['Horas']) &&
    isset($_GET['Minutos']) &&
    isset($_GET['Segundos']) &&
    isset($_GET['Nombre']))
    {
        if( empty($_GET['Nombre']) ||
            empty($_GET['Horas']) ||
            empty($_GET['Minutos']) ||
            empty($_GET['Segundos']) ||
            empty($_GET['Duracion'])) 
            {
                echo 'No pueden haber campos vacíos'; 
                ?><a href='index.php'>Volver</a><?php
            }
            else
            {
                $errorHoras;
                $errorMinutos;              
                $errorSegundos;
                $errorDuracion; 

                if(!ctype_digit($_GET['Horas'])){
                    $errorHoras = 'Horas debe tener un valor entero';
                }
                if(!ctype_digit($_GET['Minutos'])){
                    $errorMinutos = 'Minutos debe tener un valor entero';
                }
                if(!ctype_digit($_GET['Segundos'])){
                    $errorSegundos = 'Minutos debe tener un valor entero';
                }
                if(!ctype_digit($_GET['Duracion'])){
                    $errorDuracion = 'Minutos debe tener un valor entero';
                } 

                if(empty($errorHoras) && empty($errorMinutos) && empty($errorSegundos) && empty($errorDuracion)){
                    //Inicializar
                    $valorHora=0;
                    $porcentajeRecargoDiurno=0;
                    $porcentajeRecargoNocturno=0;

                    $horaDeEntrada=$_GET['Horas'];
                    $duracion=$_GET['Duracion'];
                    $horaDeSalida=$horaDeEntrada + $duracion;

                    //La ocupación puede tener 3 opciones, dos sencillas y una doble
                    if(isset($_GET['Desarrollador']))
                    {
                        $valorHora=15000;
                        $porcentajeRecargoDiurno=0.07;
                        $porcentajeRecargoNocturno=0.02;
                    }

                    if(isset($_GET['Diseñador']))
                    {
                        $valorHora=18000;
                        $porcentajeRecargoDiurno=0.05;
                        $porcentajeRecargoNocturno=0.02;
                    }

                    if(isset($_GET['Diseñador'])&& isset($_GET['Desarrollador']))
                    {
                        $valorHora=20000;
                        $porcentajeRecargoDiurno=0.1;
                        $porcentajeRecargoNocturno=0.1;
                    }

                    //calcular horas diurnas y nocturnas
                    //cantidad de evaluaciones
                    $e=$duracion+1;
                    //variable para hacer un ciclo
                    $j=$horaDeEntrada;
                    $i=1;
                    $horasNocturnas=0;

                    while ($i < $e) {
                        if($j>=22){
                            $horasNocturnas = $horasNocturnas + 1;
                        }
                        $j = $j + 1;
                        $i = $i + 1;
                    }

                    $horasDiurnas = $duracion - $horasNocturnas;


                    $salarioBasico = $valorHora * $duracion;                
                    $recargoDiurno = (($valorHora * $porcentajeRecargoDiurno)* $horasDiurnas);
                    $recargoNocturno = (($valorHora * $porcentajeRecargoNocturno)* $horasNocturnas);
                    $salarioNeto = $salarioBasico + $recargoDiurno + $recargoNocturno;

                    echo 'Hora de salida '.$horaDeSalida.'<br>'.
                    'Valor Hora '.$valorHora.'<br>'.
                    'Salario básico '.$salarioBasico.'<br>'.
                    'Porcentaje Recargo Diurno '.$porcentajeRecargoDiurno.'<br>'.
                    'Horas diurnas '.$horasDiurnas.'<br>'.
                    'Recargo diurno '.$recargoDiurno.'<br>'.
                    'Porcentaje Recargo Nocturno '.$porcentajeRecargoNocturno.'<br>'.
                    'Horas nocturnas '.$horasNocturnas.'<br>'.
                    'Recargo nocturno '.$recargoNocturno.'<br><br>'.

                    'Salario neto '.$salarioNeto.'<br>'.
                    'Nombre '.$_GET['Nombre'].'<br>'.
                    '<img src="'.$_GET['Nombre'].'.jpg" width="50" height="50" />';
                }
                else {
                    echo $errorHoras.'<br>'.
                    $errorMinutos.'<br>'. //Error aquí
                    $errorSegundos.'<br>'. //Error aquí
                    $errorDuracion.'<br>';  //Error aquí
                } 
            } //else del if empty
     }
     else
     {
        echo 'Error, variables no definidas ';
        ?><a href='index.php'>Volver</a><?php


     }



?>
    
asked by Jhon Hernández 07.06.2018 в 18:20
source

1 answer

0

The issue comes when there is no error / value to assign to these variables; for that reason they give you back that.

The solution can be with the use of objects or arrangements, but given the example provided, for speed and simplicity I recommend arrangements.

$errores = [];

if(!ctype_digit($_GET['Horas'])){
    $errores['horas'] = 'Horas debe tener un valor entero';
}
if(!ctype_digit($_GET['Minutos'])){
    $errores['minutos'] = 'Minutos debe tener un valor entero';
}
if(!ctype_digit($_GET['Segundos'])){
    $errores['segundos'] = 'Minutos debe tener un valor entero';
}
if(!ctype_digit($_GET['Duracion'])){
    $errores['duracion'] = 'Minutos debe tener un valor entero';
}

So that in the end instead of returning a concatenated string, you could only return the values in the array if they exist.

Given the solution to the problem, it does not seem the most optimal; but for this I recommend the readings of:

answered by 07.06.2018 / 18:38
source