-4

My issue is I have two numbers in the array 5,12 my issue is the code below is returning "1" as true because of the "1" in number 12- it sees the number 1 in 12. How do I tell it to read the ENTIRE number.

<?php 
$pos = strpos($foo,"1");

if($pos === false) {
    // do this if its false
    echo "<img src='../PICS/no.png' width='20' height='20' />"; 
}
else {
    echo "<img src='../PICS/yes.png' width='20' height='20' />"; 
} 
?>
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252

2 Answers2

4

Here's the signature for the function you are using:

int strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )

If you are feeding it with a array as first argument...

<?php
$foo = array(5,12);
$pos = strpos($foo,"1");

... the array will be cast to string and you'll get a notice...

Warning: strpos() expects parameter 1 to be string, array given

The resulting string will contain "Array", literally:

var_dump( @(string)array(5,12) ); // string(5) "Array"

And your search for 1 will always fail because Array does not contain it.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
3

Use in_array()

if (in_array(1,$myarray)){
    // found in the array
}
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
Brett Gregson
  • 5,867
  • 3
  • 42
  • 60