How to compare numbers using PHP?

Asked

Viewed 77 times

-1

Have $valores = [0,1,3,4,5,30] and I wanted to check if the number exists 30.

Currently use strpos()

but he also checks the 0 and the 3 and I wanted only the 30.

I’ve tried separating by commas, by quotation marks, it doesn’t work.

Is there any other way?

1 answer

1


If you have $valores = [0,1,3,4,5,30], you have an array, you can use the in_array:

$valores = [0,1,3,4,5,30];

$temTrinta = in_array(30, $valores, true);

if ($temTrinta) {
   echo "Tem o valor 30 no array";
}

You can also use the for or foreach and use a if inside, comparing each item to the desired value.

Browser other questions tagged

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