Strpos Issue with 0==False

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']

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.

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 function not working with special character

is a multi byte character and strpos cannot understand this.

What you are looking for is mb_strpos. This function performs the same action, but is multi-byte characters safe.


Update: While the answer above is correct, it was not the entire solution to your case. The problem is you are making the if check too complicated.

You are first inverting the result of strpos and then checking if it does not match false. I don't think this was your intention.
To fix this, just check if the result of mb_strpos equals false.

if (mb_strpos($name_to_check, '★ StatTrak™') === false)
echo 'not contain';
} else {
echo 'contain';
}

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.

Simple PHP strpos function not working, why?

When in doubt, read the docs:

[strpos] Returns the numeric position of the first occurrence of needle in the haystack string.

So you want to try something more like:

// ...
if (strpos($link, $unacceptable) !== false) {

Because otherwise strpos is returning a number, and you're looking for a boolean true.

strpos is not matching

The issue lies in strpos. http://php.net/manual/en/function.strpos.php

The haystack is the first argument and the second argument is the needle.

You should also do a === comparison for getting 0.

// test array
$arr = array('Title,11,11','Would,22,22','Post,55,55','Ask,66,66');
// define search function that you pass an array and a search string to
function search($needle,$haystack){
//loop over each passed in array element
foreach($haystack as $v){
// if there is a match at the first position
if(strpos($v,$needle) === 0)
// return the current array element
return $v;
}
// otherwise retur false if not found
return false;
}
// test the function
echo search("Would",$arr);

Strpos always gives true

Try this

if (strpos($links, 'http') === false) {
$linkai = 'http://'.$links;
}

In strpos documentation says return value not Boolean always.

"Warning
This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function."



Related Topics



Leave a reply



Submit