PHP 5 Strpos() Difference Between Returning 0 and False

php 5 strpos() difference between returning 0 and false?

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.

strpos issue with 0==false?

From http://www.php.net/manual/en/function.strpos.php:

Warning

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.

You have to use the === operator instead of ==.

In your case, instead of using <>, use !==:

strpos($grafik['data'], $ss1) !== false

This will return TRUE if $ss1 is found in $grafik['data']

why is a strpos that is !== false not true?

Because strpos() never returns true:

Returns the position of where the needle exists relative to the beginning of the haystack string (independent of offset). Also note that string positions start at 0, and not 1.

Returns FALSE if the needle was not found.

It only returns a boolean if the needle is not found. Otherwise it will return an integer, including -1 and 0, with the position of the occurrence of the needle.

If you had done:

if(strpos($a,'is a') == true) {
echo 'True';
}

You would have usually gotten expected results as any positive integer is considered a truthy value and because type juggling when you use the == operator that result would be true. But if the string was at the start of the string it would equate to false due to zero being return which is a falsey value.

an elegant way to handle returning strpos 0 as TRUE

strpos returns false if the needle doesn't exist within the haystack. By default (using non-strict comparison), PHP will treat 0 and false as equivilant. You need to use strict comparison.

var_dump (strpos ('The quick brown fox jumps over the lazy dog', 'dog') !== false); // bool (true)
var_dump (strpos ('The quick brown fox jumps over the lazy dog', 'The') !== false); // bool (true)
var_dump (strpos ('The quick brown fox jumps over the lazy dog', 'cat') !== false); // bool (false)

strpos returns true always in php

strpos() returns FALSE if the token was not found and the (first) position of the token in string if it was found. You need to check for boolean FALSE using the strict comparison operator === to identify if a token was found in a string or not:

if(strpos(strtolower($msg),strtolower($token)) !== false){
echo 'ok';
} else {
echo 'not ok';
}

This is because of PHP's loose typing system. If you use >=0, and the token was not found, PHP would cast the FALSE return value of strpos to 0 before the >= operation. And 0 >=0 evaluates to TRUE.

stripos return false on number comparison

You can cast to string yourself:

(string)$find

Feel free to add any check that makes sense for you, since blind casting is not a good idea:

$find = true;
var_dump((string)$find);
string(1) "1"

Why am I getting this unexpected result when using php strpos function?

Strpos returns the position of a given string (needle) in other string (stack). See reference - strpos. Correct usage of strpos (notice it's !== not !=, since we want to also check for type):

$string = 'abc:def';
echo strpos($string,'abc:') !== false ? 'abc: true' : 'abc: false';
echo ' / ';
echo strpos($string,':def') !== false ? ':def true' : ':def false';

Summing up, strpos returns the numeric value that is a position (so for example 0 or 5) or false when the value is not found.

As to why your snippet

echo strpos($string,':def') ? ':def true' : ':def false'; 

returns true - in PHP every non-zero integer is treated as true, if you are comparing it as boolean, and your strpos returned value bigger than zero (probably '4' in this example), so it was considered true. See here for more details.

strpos() returning false, when needle is in haystack

It is returning 0, not false. In the if test PHP evaluates integer value 0 as boolean value false.

Use strpos($url, "http://") !== false to check both value and data type. It is called strict comparison.

Manual: Comparison Operators and strpos().



Related Topics



Leave a reply



Submit