odd behavior of stripos() with === operator

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

 



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 

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