September 2001
Intermediate to advanced
768 pages
32h 45m
English
Many functions that find substrings or position of a substring within another string return FALSE when the specified substring is not found. This is appropriate behavior; however, due to PHP’s loose typing, FALSE and 0 can appear to be the same value. Consider this code snippet:
<?php $string = 'Jacob Two-Two Meets the Hooded Fang'; $substring = 'Jacob'; // The wrong way to do it - Jacob is found at position 0 // and the while loop exits before running the body of the loop $pos = 0; while ($pos = strpos ($string, $substring, $pos)) { echo "Found '$substring' at position $pos\n"; $pos += strlen ($substring); } // A better way to do it - explicitly test for FALSE // using the strict 'not equals' comparison operator ...Read now
Unlock full access