php strpos() difference between returning 0 and false?
Asked Answered
C

5

26
if(strpos("http://www.example.com","http://www.")==0){ // do work}

I'd expect this to resolve as true, which it does. But what happens when I do

if(strpos("abcdefghijklmnop","http://www.")==0){// do work}

This also passes on php 5 because as far as I can work out the strpos returns false which translates as 0.

Is this correct thinking/behaviour? If so what is the workaround for testing for that a substring is at the beginning of another string?

Clathrate answered 7/3, 2010 at 15:13 Comment(0)
P
65

Yes, this is correct / expected behavior :

  • strpos can return 0 when there is a match at the beginning of the string
  • and it will return false when there is no match

The thing is you should not use == to compare 0 and false ; you should use ===, like this :

if(strpos("abcdefghijklmnop","http://www.") === 0) {

}

Or :

if(strpos("abcdefghijklmnop","http://www.") === false) {

}


For more informations, see Comparison Operators :

  • $a == $b will be TRUE if $a is equal to $b.
  • $a === $b will be TRUE if $a is equal to $b, and they are of the same type.

And, quoting the manual page of strpos :

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.

Pandarus answered 7/3, 2010 at 15:15 Comment(0)
A
14

=== and !== compare type and value as shown below:

if (strpos("abcdefghijklmnop", "http://www.") !== false) {
     // do work     
}
Ameba answered 7/3, 2010 at 15:15 Comment(1)
for strpos() this answer is the bestTuraco
H
5

strpos returns an int or boolean false. the == operator also evaluates 0 to mean false, you want to use the === operator (three equals signs) that also checks that the types being compared are the same instead of just seeing if they can be evaluated to mean the same.

so

if (strpos($hastack, $needle) === 0)
{
    // the $needle is found at position 0 in the $haystack
}
Harrier answered 7/3, 2010 at 15:17 Comment(0)
P
1

0 is a possible return value from strpos when it finds a match at the very beginning. In case if the match is not found it returns false(boolean). So you need to check the return value of strpos using the === operator which check the value and the type rather than using == which just checks value.

Pressroom answered 7/3, 2010 at 15:17 Comment(0)
S
-2

I personally tend to use this way :

if(!strpos($v,'ttp:'))$v='http://'.$v;

or

if(strpos(' '.$v,'http'))

to avoid the "0" position then always make it a number more than 0 cheers

Schneider answered 13/1, 2013 at 12:58 Comment(2)
If your code uses non-standard ways in non-standard notation to fix standard problems all maintainability has left the building. Every decent PHP developer knows and expects === notation - your answer just presents an obsolete performance and readability killer.Dari
I consider this pseudo code at best. Readers, consider the appropriate return types on this built in function.Potential

© 2022 - 2024 — McMap. All rights reserved.