By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,723 Members | 975 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,723 IT Pros & Developers. It's quick & easy.

Issues with strpos

P: n/a
Not quite sure what's happening here, the docs for strpos() say:

"Returns the position as an integer. If needle is not found, strpos()
will return boolean FALSE."

Looks fine, I can simply do:

if(strpos("michael", "m")

But then the docs go on to say:

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

This seems a little inconsistent to me, means I can't do the
following:

if(strpos("michael", "z") >= 0)
{
//found it!
}

Because that always returns as found, which clearly it isn't. I can
use comparison operators to check for type, but only using "===" or "!
==".

I only want a function to tell me if the string was found (>= 0), I
don't care which position it was etc

What do others use to do this? Preferably without converting to an
array then doing an array find or something.
Jun 2 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On May 31, 10:58 am, ljb <ljb1...@pobox.comwrote:
sha...@gmail.com wrote:
Not quite sure what's happening here, the docs for strpos() say:
"Returns the position as an integer. If needle is not found, strpos()
will return boolean FALSE."
Looks fine, I can simply do:
if(strpos("michael", "m")
But then the docs go on to say:
"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."
...
I only want a function to tell me if the string was found (>= 0), I
don't care which position it was etc

Use: if (strpos("michael", "m") !== False) ...
Thanks ljb
Jun 2 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.