RE: odd behavior of stripos() with === operator *LAST POST*

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxxxxx
>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
>  

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux