RE: [PHP] odd behavior of stripos() with === operator *LAST POST*
| From: | Michael | Date: | Tue, 21 Nov 2006 00:23:12 +0000 |
| Subject: | RE: [PHP] odd behavior of stripos() with === operator *LAST POST* | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-244924@lists.php.net to get a copy of this message | ||
At 10:12 AM 11/17/2006 , Ford, Mike wrote:
>On 17 November 2006 16:50, Stut wrote:
>
>> > > Your basic misunderstanding is that === is the opposite of !==
>> > > which it's not.
>
>Complete rubbish -- it so absolutely is!
>
>If $a===$b, then !($a===$b) is the same as $a!==$b, QED.
>
>
>> > > (INTEGER === true) will always be false because the types
>> > > don't match
>> > > (INTEGER !== true) will always be true because the types
>> > > don't match
>> > > (INTEGER === false) will always be false because the types
>> > > don't match
>> > > (INTEGER === true) will always be false because the types
>> > > don't match
>
>Well, you haven't covered all bases there, you've got INTEGER===true in twice!
>
>Try this:
>
> (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, do not do this :
if ( stripos("abcde","abc")=== TRUE ) {echo "abc found!"}
this will NEVER be true.
you can however do this: ( (stripos("abcde","abc")!== FALSE) ) { echo "abc
found" }
in other words do not try to test for === TRUE, only test for !== FALSE
and the warning in the manual ...
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.
should read "...use the !== operator for testing..."
because if you use ===, you will NOT get the expected results if needle is
found in haystack at position 0. However, !== will always work correctly.
>
>Looks seriously like two sets of complementary results there to me.
>
>
>> > > The actual value of the INTEGER does not matter.
>
>True.
>
>> > > But the basic thing to get clear in your head is that === and !==
>> > > are not opposites in the same way that == and != are.
>
>False, false, false, and a thousand times false. If $a===$b returns TRUE,
then $a!==$b returns FALSE; and if $a===$b returns FALSE, then $a!==$b returns
TRUE. I don't know how much more opposite you can get.
>
>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
>
>--
>PHP General Mailing List (http://www.php.net/)
>To unsubscribe, visit: http://www.php.net/unsub.php
>