Doubt in the comparison of variables

0

Is there another way to develop this comparison?

$numero = 1234;
$a = 1111;
$b = 2222;
$c = 3333;
$d = 4444;

if ($numero == $a or $numero == $b or $numero == $c or $numero == $d) {
    echo "O número existe";
    } else {
    echo "Não existe o número";
}

I wanted to know if there is another way to manipulate this comparison

  ($numero == $a or $numero == $b or $numero == $c or $numero == $d)
    
asked by anonymous 16.04.2014 / 23:09

1 answer

3

If you need to find (compare) a value, you can transform these variables ( $a, $b, $c e $d ) into an array and then search with in_array () , the first argument ( $numero ) is what you want to fetch and the second ( $arr ) in which variable this should be done.

$numero = 1234;
$a = 1111;
$b = 2222;
$c = 3333;
$d = 4444;

$arr = array($a, $b, $c, $d);

if (in_array($numero, $arr)) {
    echo "O número existe";
} else {
    echo "Não";
}
    
16.04.2014 / 23:19