PHP does not recognize the auxiliary variables

-1

Good, the problem I have is the following: In an exercise I am forced to use only if-else to know which of the three numbers is the major / minor.

The error is that I do not pick up the auxiliary variables, and I do not know why: My code:

EDIT

I can not use operators like AND, NOT ... etc, just comparing values.

<?php
        //Declaración de variables que se introducen del formulario     
                $num1 = $_POST['num1'];
                $num2 = $_POST['num2'];
                $num3 = $_POST['num3'];
        //Muestra de las variables introducidas
                echo "Los números introducidos son $num1,  $num2 y $num3";
        //Condiciones del ejercicio
                if ($num1 > $num2) {
                        $aux=$num1;
                }
                        elseif ($aux > $num3  ) {
                                echo"$num1 es el mayor de todos";
                                }
                        elseif ($num1 < $num2) {
                                $aux2=$num1;
                                }
                        elseif ($aux2 < $num3) {
                                echo"$num1 es el menor de todos";
                                }
                        elseif ($num2 > $num1) {
                                $aux3=$num2;
                                }
                        elseif ($aux4 > $num3) {
                                echo"$num2 es el mayor de todos";
                                }
                        elseif ($num2 < $num1) {
                                $aux4=$num2;
                                }
                        elseif ($aux4 < $num3) {
                                echo"$num2 es el menor de todos";
                                }
                        elseif ($num3 > $num1) {
                                $aux=$num3;
                                }
                        elseif ($aux5 > $num2) {
                                echo"$num1 es el mayor de todos";
                                }
                        elseif ($num3 < $aux2) {
                                echo"$num3 es el menor de todos";
                                }
                        else {
                                echo"Todos los números son iguales";
                        }

?>
    
asked by ras212 20.10.2016 в 23:48
source

2 answers

2

Auxiliary variables are for saving data that we need but then we know that they can change. As your values do not change but compare with other numbers, you just have to compare the numbers with each other and find the largest and the least.

A very simple case is when you want to exchange values between two variables eg:

$numero_uno = 1;
$numero_dos = 2;

How do I change them?

$numero_auxiliar = $numero_uno;
$numero_uno = $numero_dos;
$numero_dos = $numero_auxiliar;

Example:

<?php
        $numero_uno = 1;
        $numero_dos = 2;
        echo "numero uno ".$numero_uno;
        echo "<br><br>";
        echo "numero dos ".$numero_dos;
        echo "<br><br>";
        $numero_auxiliar = $numero_uno;
        $numero_uno = $numero_dos;
        $numero_dos = $numero_auxiliar;
        echo "<br><br>";
        echo "numero uno ".$numero_uno;
        echo "<br><br>";
        echo "numero dos ".$numero_dos;
 ?>

Then to solve your problem, you will replace the values of the $num1 , $num2 and $num3 with the values $_POST you want.

<?php
    $num1 = 54;
    $num2 = 6;
    $num3 = 6;
    $mayor = 0;
    $menor = 0;

    if($num1 > $num2){
            if($num1 > $num3){
                    $mayor = $num1;
            }else{
                    $mayor = $num3;
            }
    }else{
            if($num2 > $num3){
                    $mayor = $num2;
            }else{
                    $mayor = $num3;
            }
    }
    if($num1 < $num2){
            if($num1 < $num3){
                    $menor = $num1;
            }else{
                    $menor = $num3;
            }
    }else{
            if($num2 < $num3){
                    $menor = $num2;
            }else{
                    $menor = $num3;
            }
    }
    if($menor == $mayor){
            echo "los numeros son iguales";
    }else{
            echo "El numero mayor es $mayor";
            echo "<br><br>";
            echo "El numero menor es $menor";
    }
?>
    
answered by 21.10.2016 / 00:03
source
0
<?php 
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$num3 = $_POST['num3'];

$max = $num1;
$min = $num2;

if($num2 > $max) {
    $max = $num2;
    $min = $num1;
} elseif($num3 > $max) {
    $max = $num3;
} elseif ($num3 < $min) {
    $min = $num3;
}

if ($num1 == $num2 && $num1 == $num3) {
    echo "Todos los números son iguales";
} elseif {
    echo "El $max es el número más grande";
    echo "El $min es el número más pequeño";
}
?>
    
answered by 21.10.2016 в 00:11