View Single Post
  #4 (permalink)  
Old 08-08-2007, 04:26 PM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
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."

__________________
"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off."
-- Bjarne Stroustrup, creator of what is now known as C++
For more quotes by Bjarne Stroustrup, check out http://www.research.att.com/~bs/bs_faq.html#really-say-that.
Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to rpgfan3233 For This Useful Post:
Lee (08-08-2007)