Languages like Java and Javascript provide a method called indexOf() that returns an int of the first position of the string key found in a string. strpos() in PHP acts differently. If it finds the substring within the string, it returns an int value of the position of the substring. If it does not, it returns a boolean instead.
I find this stupid. It would have been easier if it still returned an int value like the usual -1 to indicate it didn’t find any substring inside the string.
To do an if statement using strpos() of PHP just like this Java code:
|
1 2 |
String str = "This is an object"; if (str.indexOf("an") > -1) System.out.println("substring found"); |
Do this in PHP:
|
1 2 |
$str = "This is an object"; if (strpos($str, "an") !== true) echo "substring not found"; // if statement is false |
Notice the 2 equal sign succeeding after the exclamation point? That line checks if the statement is false. If you wish to change it to a true condition statement, just put in 3 equal signs.
your posts seems to be wrong!
!== true in strpos is not correct
you need to check on === false