Odd or even array in PHP

Asked

Viewed 5,008 times

10

I need to create a array and tell if the values within it are even or odd. I made the code that way:

<?php
$par_ou_impar = array(2,3,4,56,5,42,98,100);

 for ($i = 0; $i < count($par_ou_impar); $i++){
    if ($par_ou_impar % 2 == 0)
        echo "O Número é par: $par_ou_impar[$i]<br />";
    else
        echo "O Número é impar: $par_ou_impar[$i]<br />";
 }
?>

He shows me all the values that are inside the array on the screen, but the result came out as odd.

4 answers

11


It’s simple, the check is comparing to the array and not with the element of array, as it should. That is, it always turns out false because a array, as a whole is always different from 0. Missing brackets with the index to catch the element:

$par_ou_impar = array(2,3,4,56,5,42,98,100);
for ($i = 0; $i < count($par_ou_impar); $i++) {
    if ($par_ou_impar[$i] % 2 == 0) echo "O Número é par: $par_ou_impar[$i]<br />";
    else echo "O Número é impar: $par_ou_impar[$i]<br />";
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Since you are learning, I suggest using a control that will avoid the mistake made:

$par_ou_impar = array(2, 3, 4, 56, 5, 42, 98, 100);
foreach ($par_ou_impar as $item) {
    if ($item % 2 == 0) echo "O Número é par: $item<br />";
    else echo "O Número é impar: $item<br />";
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

The foreach sweeps all over the array. Almost always this is what you want. It controls it best for you. It is always recommended to use it when possible.

Can simplify even more:

$par_ou_impar = array(2, 3, 4, 56, 5, 42, 98, 100);
foreach ($par_ou_impar as $item) echo "O Número é " . ($item % 2 == 0 ? "par: " : "impar: ") . $item . "<br>";

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

In some circumstances you can use the function each().

  • Thanks friend, it worked out here! Thanks for the explanation.

9

You missed a detail, using [$i] here:

if ($par_ou_impar % 2 == 0)
                ^

to compare with the element being iterated. So the right code will be:

$par_ou_impar = array(2,3,4,56,5,42,98,100);

for ($i = 0; $i < count($par_ou_impar); $i++){
    if ($par_ou_impar[$i] % 2 == 0)
        echo "O Número é par: $par_ou_impar[$i]<br />";
    else
        echo "O Número é impar: $par_ou_impar[$i]<br />";
}

example: https://ideone.com/OPnlAa

  • Thanks a lot, thanks a lot!

5

I leave my contribution with array_map

function checarNumeros($numeros){
   return ($numeros % 2 ) ? TRUE : FALSE;
}


$parOuImpar  = array(2,3,4,56,5,42,98,100);


$array  = array_map("checarNumeros", $parOuImpar);

print_r($array);

An array is returned where ,the odd return 1 and the pairs return 0.

  • I don’t understand the down note

3

I know it’s been taken care of, but I’ll leave you an alternative:

<?php
   $par_ou_impar = array(2,3,4,56,5,42,98,100);
   for ($i = 0; $i < count($par_ou_impar); $i++){
       echo ($par_ou_impar[$i] % 2) ? "{$par_ou_impar[$i]} é impar\n" : "{$par_ou_impar[$i]} é par\n";
   }
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.