Re: odd behavior of stripos() with === operator *UPDATE*

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

 



At 12:24 AM 11/17/2006 , Michael wrote:
>HEllo all,
>
>After pulling my hair out for several hours trying to figure out why my code
>wasn't working I built this little test and ran it, the results are interesting
>in the least, and to me, surprising. It is possible that I have done something
>wrong, but I checked and rechecked this in the documentation.
>
>It appears there is either a problem with the === operator (or my brain...)
>If you don't mind, I'd like to see what you all think.
>
>I am running php 5.2.0 on a linux redhat 9 box with Apache 2.2 (the php 5.2.0
>install is brand new, perhaps I set it up wrong?)
>
>anyway here is the code I wrote, and the output from it...
>
>     $found = stripos("abcdefg", "abcdefg");
>echo "found = stripos(\"abcdefg\", \"abcdefg\");\n"
>echo "The value of found is = : $found\n"
>
>// 1a) --- needle was found in haystack THIS SHOULD BE 
>     if ( $found !== FALSE ) {
>      echo "found does not equal FALSE\n"
>     }
>// 1b) --- needle was found in haystack THIS SHOULD ALSO BE
>     if ($found === TRUE )  {
>      echo "found is equal to TRUE\n"
>     }
>
>//1c) --- needle was NOT found in haystack THIS SHOULD NOT BE
>     if ( $found === FALSE )  {
>      echo "found is equal to FALSE\n"
>     }
>//1d) --- needle was NOT found in haystack THIS ALSO SHOULD NOT BE
>     if ($found !== TRUE )  {
>      echo "found does not equal TRUE\n"
>     }
>
>     $found = stripos("abcdefg", "tuvwxyz");
>
>echo "\$found = stripos(\"abcdefg\", \"tuvwxyz\");<br>\n"
>echo "The value of found is = : $found\n"
>
>//2a) --- needle was found in haystack  THIS SHOULD NOT BE
>     if ( $found !== FALSE ) {
>      echo "found does not equal FALSE\n"
>     }
>//2b) --- needle was found in haystack THIS ALSO SHOULD NOT BE
>     if ($found === TRUE )  {
>      echo "found is equal to TRUE\n"
>     }
>
>//2c) --- needle was NOT found in haystack THIS SHOULD BE
>     if ( $found === FALSE )  {
>      echo "found is equal to FALSE\n"
>     }
>//2d) --- needle was NOT found in haystack THIS SHOULD ALSO BE
>     if ($found !== TRUE )  {
>      echo "found does not equal TRUE\n"
>     }
>
>the output:
>
>$found = stripos("abcdefg", "abcdefg");
>The value of found is = : 0 
>
>found does not equal FALSE   //this is from section 1a) of the code
>
>found does not equal TRUE    //this is from section 1d) of the code
>                             // I expected the code from 1b) to be executed
>
>$found = stripos("abcdefg", "tuvwxyz");
>The value of found is = : 
>
>found is equal to FALSE      //this is from section 2c) of the code
>
>found does not equal TRUE    //this is from section 2d) of the code
>
>I have underlined the output I am interested in... How can the variable $found
>be both TRUE and FALSE at the same time?
>
>Anyone who can provide me some insight on this, please enlighten me.
>
>If my code is correct, then this behavior of the === operator is
>counter-intuitive, it was my understanding that the === and !== operators were
>supposed to be used with the output of stripos() for just this situation, but
>=== does not appear to recognize that the returned "0" (because the string was
>found at index 0) ; whereas the !== does recognize this...
>
>is === buggy? or am I? heh
>
>thoughts? comments?
>
>Thanks all,
>Michael 
>

Hello again,

I have tested and re-tested this to the point I am confident to update this post with my findings...

given:  $haystack="abcdef" and $needle="abc"
any use of " stripos($haystack, $needle) === TRUE " will return a FALSE RESULT.

In other words, if there is ANY chance that $needle will be found at the very beginning of $haystack (index = 0), you CANNOT USE "=== TRUE", you must use "!== FALSE".

To me this behavior is counter-intuitive. If something is "!== FALSE", then "=== TRUE" should also be correct.

I understand why a string found at the beginning of another string returns an integer 0 (the index at which the needle was found in the haystack) from stripos(), however, my point is that you SHOULD be able to test this for a TRUE condition as well as for a FALSE.

I'm not sure why the === operator does not handle this condition, since the wonderful people at PHP foresaw the problem and fixed !== to handle it, I would like to see the === fixed to handle this as well (if it is even possible, not sure about how this is implemented??) 

ANyway, just posting this update for posterity, in case anyone else tries to test for === TRUE instead of !== FALSE with stripos() and checks here before they spend hours debugging their code.

If anyone sees anything I did wrong that is causing this behavior PLEASE let me know, I am no PHP "Guru" and I have no ego, and will humbly retract this post if I am wrong. I would just like to know WHY and WHERE I am wrong :)

Thank you for your time.

Regards,
Michael

ps - ok ok. I know I can just use !== FALSE, just want to let others in the community know about this :)

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