Re: strpos mystery

From: Date: Thu, 29 Jul 2004 04:26:22 +0000
Subject: Re: strpos mystery
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-192152@lists.php.net to get a copy of this message
Heck, even I got it wrong ;) True check below should always fail... Jason Barnett wrote:
Because === and !== check the type as well. Of you set $string = 'blah' you'll still get the same result. If you were using != and == both would print. strpos() returns an int, so comparing it to false with === is always false. The same would be true for true.
That's half right. strpos actually *can* return false as opposed to 0, so checking doing the === check might be necessary for your application. <?php $string = 'blah'; if (strpos($string, 'not in the original $string') === false){ echo 'False check succeeded - not in $string.'; } if (strpos($string, $string) === true) { echo 'True check succeeded - in string'; } else { echo 'True check failed - because strpos was at offset 0.'; } ?>


« previous php.general (#192152) next »