RE: [PHP] odd behavior of stripos() with === operator *LAST POST*
| From: | Ford, Mike | Date: | Tue, 21 Nov 2006 11:49:11 +0000 |
| Subject: | RE: [PHP] odd behavior of stripos() with === operator *LAST POST* | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-244946@lists.php.net to get a copy of this message | ||
On 21 November 2006 00:23, Michael wrote:
> At 10:12 AM 11/17/2006 , Ford, Mike wrote:
> >
> > (integer)===TRUE -- is always FALSE
> > (integer)!==TRUE -- is always TRUE
> >
> > (integer)===FALSE -- is always FALSE
> > (integer)!==FALSE -- is always TRUE
>
> Which brings me to the original point of the post, (which is now
> burried in a heap of discussion) a heads-up to the unwary...
>
> If there is any chance that the needle you are looking for in the
> haystack will be found at the first position (index 0), using ===
> will NEVER return true,
What rot, this is a perfectly valid piece of PHP code in which the === will return TRUE if $a does
not contain the string 'abc' (and so execute the die()):
if (strpos($a, 'abc')===FALSE):
die('Invalid string');
endif;
> do not do this :
>
> if ( stripos("abcde","abc")=== TRUE ) {echo "abc found!"} this
> will
> NEVER be true.
No of course not -- why in the world would you do that when TRUE is not one of the documented return
values of strpos()??
Sorry, but I think you're still missing the essential point, which is that the opposite of
testing for !==FALSE is *not* ===TRUE, but ===FALSE -- in other words, !($x!==FALSE) is the same as
($x===FALSE), or in other other words you invert *only* the *test*, not the *operand*. And
it's like this because === and !==, unlike == and !=, do not coerce their operands to Boolean,
so something that is !==FALSE is not *necessarily* ===TRUE (although it *is* necessarily ==TRUE).
Cheers!
Mike
---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@leedsmet.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211
To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm