View Single Post
Old 09-01-2007, 11:29 PM  
sortie
Confirmed User
 
sortie's Avatar
 
Industry Role:
Join Date: Mar 2007
Posts: 7,771
Quote:
Originally Posted by GrouchyAdmin View Post

If you have:

Code:
$data="hello";
$testfor="h";
$test=strpos($data, $testfor);
echo ($test) ? "'$testfor' found in '$data'.\n" : "'$testfor' not found in '$data'.\n";
It would say 'h' not found in 'hello', because the 'h' is at the beginning, or offset 0, which also means FALSE to PHP.
That's does not look like an error or anything strange to me.
I don't do php, so tell me if I'm wrong:

You tested for the "string position" of "h" and it retuned position zero, which means it was found!! The first index of a string is "0", so it found "h" in the very first index of the string "hello".

Then you do a boolean test on the result for the "echo", correct?.

You can only do a boolean test of a result that will return either 0 or 1 and nothing else.

Change the "h" to "o"

$testfor="o";
$test=strpos($data, $testfor);

And it returns "4" right?

If the "h" was not found then the function either returns "-1" or "string::nopos", right?

Neither of those are boolean values.

Your example doesn't illustrate a problem with php, instead it illustrates that you tried to do a boolean test on a non-boolean result.

Right???
__________________
sortie is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote