PHP comparison does not work

1

I'm having a problem, the comparison is not working, it should return the "All Right" message but only returns the message "Valid only numbers from 1 to 60"

    <?php 

    $a = array(1,2,3,4,60);
    $b = count($a);
    $u = count(array_unique($a));

    echo "Original: ".$b."<br>"."Unicos: ".$u."<br>";
    if($b === $u){
        foreach($a as $aceitos){
            if($aceitos > 0 AND $aceitos < 61) {
                echo "Validos apenas números de 1 a 60";
            }else{
                echo "Tudo certo";
            }
        }
        echo "Arrays iguais";
    }else{
        echo "Arrays diferentes";
    }

 ?>
    
asked by anonymous 01.01.2017 / 21:47

2 answers

2

You just messed up there in the condition

    if($aceitos > 0 AND $aceitos < 61) {
            echo "Validos apenas números de 1 a 60";
        }

In this condition, it would be the numbers that would be accepted, in the case between 0 and 60, but the message that you are in reverse, because if you entered this condition, the number is correct. Either you invert the if with the else, or you do it this way:      

    $a = array(1,2,3,4,60);
    $b = count($a);
    $u = count(array_unique($a));

    echo "Original: ".$b."<br>"."Unicos: ".$u."<br>";
    if($b === $u){
        foreach($a as $aceitos){
            echo $aceitos;
            if($aceitos < 0 || $aceitos > 60) {
                echo "Validos apenas números de 1 a 60<br>";
            }else{
                echo "Tudo certo";
            }
        }
        echo "Arrays iguais";
    }else{
        echo "Arrays diferentes";
    }

 ?>

I hope I have helped

    
01.01.2017 / 22:44
1

You confused the conditional na.

if($aceitos > 0 AND $aceitos < 61)

should be

if($aceitos < 1 OR $aceitos > 60)

    
01.01.2017 / 21:55