arrays with if php

2

Well I am something new and I do not have much exp the following thing in an arrangement of 3 numbers to know which is the biggest, the smallest or if they are equal I have it this way I do not know if it will be correct or open a better clarification I have to do this with conditional if, else

<?php 
    $numm=array();

    $numm[0]=1;
    $numm2[1]=2;
    $numm3[2]=3;

    if($numm[0]<$numm2[1] or $numm3[2])
    {
        echo "El indice menor es:".$numm[0];

    }
    if($numm3[2] > $numm2[1] or $numm[0])
    {
        echo "El indice mayor es:".$numm3[2];
    }
    else
        {
            echo "todos son iguales";
        }

 ?>
    
asked by Shein 06.10.2018 в 07:20
source

2 answers

2

I would do it in the following way:    


<?php 

    $numeros = [1,2,3];
    $mayor = 0;
    $menor = PHP_INT_MAX;

    foreach($numeros as $item){
      if($item > $mayor){
       $mayor = $item;
      }
      if($item < $menor){
       $menor = $item;
      }
    }
   if($menor === $mayor){
      echo "Todos son iguales";
   }else{
     echo "El menor es: ". $menor;
     echo "\nEl mayor es: ". $mayor;
   }
?>

    
answered by 06.10.2018 в 07:50
2

I do not know why you put the "or" but if it's only with if else, then you could do something like this:

I clarify something, I do not know if they told you that you can nest the if / else, so I will do it without nesting them.

<?php 
    $numm=array();
    $numm[0]=1;
    $numm[1]=2;
    $numm[2]=3;
    if(($numm[0]==$numm[1]) && ($numm[0]==$numm[2])){
        echo "Todos los numeros son iguales";
    }
    if(($numm[0]<$numm[1]) && ($numm[0]<$numm[2])){
        echo "El menor de los numeros es ".$numm[0];
    }   
    if(($numm[0]<$numm[1]) && ($numm[0]==$numm[2])){
        echo "Hay dos numeros menores iguales a: ".$numm[0];
    }   
    if(($numm[0]==$numm[1]) && ($numm[0]<$numm[2])){
        echo "Hay dos numeros menores iguales a: ".$numm[0];
    }
    if(($numm[1]<$numm[0]) && ($numm[1]<$numm[2])){
        echo "El menor de los numeros es ".$numm[1];
    }   
    if(($numm[1]<$numm[0]) && ($numm[1]==$numm[2])){
        echo "Hay dos numeros menores iguales a: ".$numm[1];
    }   
    if(($numm[2]<$numm[0]) && ($numm[2]<$numm[1])){
        echo "El menor de los numeros es ".$numm[2];
    }   
?>

Corrections to your code:

$numm2[1]=2;
$numm3[2]=3;

By:

$numm[1]=2;
$numm[2]=3;

Reason:

There are no arrays numm2 and numm1

This part:

    $numm=array();
    $numm[0]=1;
    $numm[1]=2;
    $numm[2]=3;

It can be changed by this, since it is basically the same:

    $numm=array(1,2,3);

And I change the or (you should have put || since that is the or in php) that you used for && (and) and check all the possible cases.

I explain to you every if:

If everyone is the same:

if(($numm[0]==$numm[1]) && ($numm[0]==$numm[2])){
    echo "Todos los numeros son iguales";
}

If the first number is the smallest:

if(($numm[0]<$numm[1]) && ($numm[0]<$numm[2])){
    echo "El menor de los numeros es ".$numm[0];
}

If the first and the third are equal and are the minor ones:

if(($numm[0]<$numm[1]) && ($numm[0]==$numm[2])){
    echo "Hay dos numeros menores iguales a: ".$numm[0];
} 

If the first and second are the same and are the least:

if(($numm[0]==$numm[1]) && ($numm[0]<$numm[2])){
    echo "Hay dos numeros menores iguales a: ".$numm[0];
}

If the second is the least of all:

if(($numm[1]<$numm[0]) && ($numm[1]<$numm[2])){
    echo "El menor de los numeros es ".$numm[1];
}  

If the second and third are equal and are the minor ones:

if(($numm[1]<$numm[0]) && ($numm[1]==$numm[2])){
    echo "Hay dos numeros menores iguales a: ".$numm[1];
}   

If the third party is the youngest of all:

if(($numm[2]<$numm[0]) && ($numm[2]<$numm[1])){
    echo "El menor de los numeros es ".$numm[2];
}   
    
answered by 06.10.2018 в 23:27