PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
$my_array = array('pop','rock');
print_r($my_array);
if(array_search('pop',$my_array))
{
echo "String found";
}
else
{
echo "String not found in the array";
}
The out put shows "String not found in the array"..
Array
(
[0] => pop
[1] => rock
)
String not found in the array
In the code i am trying to check if there exists any key.. yaa a key does exist for 'pop' in the array but the value of the key is 0 which by default is taken as false by the if statement.
so it is correct to test if the function executed by using false, but not correct to test for if is in array using if (key). instead if (bool) should be used, which is what >-1 returns.
I think this is a mistake a lot of programmers make. You should always know what the return values of a function are and test for those. array_search returns false on failure. So test for that.