5

why is the following php code not working:

$string = "123";
$search = "123";

if(strpos($string,$search))
{
    echo "found";
}else{
    echo "not found";
}

as $search is in $string - shouldn't it be triggered as found?

2
  • See the first example on php.net/manual/en/function.strpos.php. You need === to compare it Commented May 23, 2011 at 15:27
  • To eschew that issue, learn to use strstr instead. Commented May 23, 2011 at 23:01

7 Answers 7

8

This is mentioned in the Manual: 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.

In your case the string is found at the index 0 and in php 0 == false

The solution is to just use the strict comparator

echo strpos($string,$search) === false
     ? "not found"
     : "found";

Another one

echo is_int(strpos($string,$search))
     ? "found"
     : "not found";

Or something ... lets say interesting :D Just for illustration. I don't recommend this one.

echo strpos('_' . $string,$search) // we just shift the string 1 to the right
     ? "found"
     : "not found";
Sign up to request clarification or add additional context in comments.

Comments

3

This is happening because the search string is being found at position 0. Try

if(strpos($string,$search) !== FALSE)

instead of

if(strpos($string,$search))

6 Comments

(1 != false) === true. In fact ($x != false) === ($x == true) === ((boolean) $x) for every $x. Thus both code snippets are identical.
@KingCrunch: so would if(strpos($string,$search) !== FALSE) be correct?
@OzairKafray it worked for me using !== false, it doesn't if I use != false
@user1301428: This was to highlight the difference between ===/!== and ==/!=, different things would work in different situations. To be sure what to use one should read the documentation for a function carefully and then assess his/her situation.
@OzairKafray if I understand it correctly, when the needle is found at the beginning of the haystack, then !== should always be used, but I might be wrong
|
2

strpos returns the first offset where $search was found - 0. 0 in turn evaluates to false. Therefore the if fails.

If $search was not found, strpos returns FALSE. First check the return value for !== FALSE, and then check the offset.

Thanks to everyone who pointed this out in the comments.

see: http://php.net/manual/en/function.strpos.php

4 Comments

... which is why the standard practice is to check whether strpos(...) === FALSE. See the big warning on the man page: php.net/manual/en/function.strpos.php
Correct explanation, but it's better to strictly check if itis' not false (!== false)
strpos($string,$search)>=0 does not work as expected, because (false >= 0) === true.
You are of course correct - you really should check !== FALSE !
2

From the manual:

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.

In your example, you should use

$string = "123";
$search = "123";

if ( false !== strpos( $string, $search ) ) {
    echo "found";
} else {
    echo "not found";
}

Comments

1

strpos returns the numeric position of the string you want to search for if it finds it. So in your case, you want to be doing this instead:

$search = "123";
$string = "123";
if (strpos($string,$search)===false) { echo "not found"; }
else { echo "found"; }

basically it returns a false if it doesn't find your string

Comments

0

You can use this:

<?php

$string = "123";

$find = "123";

$strpos = strpos($string, $find);

if($strpos || $strpos === (int)0) {
    echo "Found it!";
} else {
    echo "Not Found!";
}

?>

Comments

0

Well documented issue explained here. strpos is simply returning '0'

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.