I recently used === in a PHP script of my own. One area where === is useful is true/false v.s. non-zero and non-false values. Any non-false and non-zero value is considered true. In other words, if you are testing for a boolean value to be returned from a function (i.e. a PHP function returns FALSE on error, but its return value is defined as int), you should use === instead of == .
In fact, there is a noticeable warning present in the documentation for the
strpos function. For those that don't feel like loading a page (especially those who still use dial-up or have a similar slow connection), here is the warning:
Quote:
Warning
This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "". Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.
|
PHP Code:
$needle = 'hELLO';
$haystack = 'Hello World!';
$pos = strpos($haystack, $needle);
if ($pos === false)
echo "$needle could not be found in $haystack.";
else
echo "$needle was found at position $pos in $haystack.";
$needle = 'Hello';
$pos = strpos($haystack, $needle);
if ($pos === false)
echo "$needle could not be found in $haystack.";
else
echo "$needle was found at position $pos in $haystack.";