Home > Programming > PHP functions that return Boolean

PHP functions that return Boolean

OK, so there are several function in PHP that return Boolena true or false. So, if you want to test against them, us coders get in the habit of doing something like this:

if (!isarray($myArray)){ …..

Problem. What is that function returns an interger, OR boolen, such as strpos? You will see tons of people doing this:

if (!strpos($haystack, $needle)){ ……

Bad bad bad. This will cause all kinds of problems. What if the “needle” is found at position Zero? Zero is also interpereted as False, although in this case what you want is a True… SOOO… be good boys and girls and use the mysterious triple equals.

if (stripos($needle, $haystack) === false){ ….

or

if (stripos($needle, $haystack) !== false){ ….

You can also use a greater than -1, but I see this as sloppy. Is it actaully sloppy? Maybe not, just my feelings on the matter. I feel that if you should be as specific as you can get in your logical expression testing.
Hope that helps ya!



Chris Fontes Programming

  1. No comments yet.
  1. No trackbacks yet.

Powered by WP Hashcash