Jump to content

STRPOS not working per W3S documentation


mickeymouse

Recommended Posts

Strpos documentation says "If a match is found, the function returns the character position of the first match. If no match is found, it will return FALSE."

My code:

$mystrpos=(strpos($row[3],$choice));

print("T161: choice=$choice  - position in row[3]=$mystrpos<br>");

Results:

When $choice is found, the print result shows the character position.  example: row[3]=BAC CAS,   T161: choice=BAC - position in row[3]=0

But, when not found, it does not return "FALSE"  It returns nothing - I can't even test it.  example: row[3]=BAC CAS,   T161: choice=Fam - position in row[3]=

 

Link to comment
Share on other sites

The string representation of the boolean value false is an empty string.

You can use the strict equals operator === to test if it's false.

$mystrpos=(strpos($row[3],$choice));
if($mystrpos === false) {
    $mystrpos = "false";
}
print("T161: choice=$choice  - position in row[3]=$mystrpos<br>"); 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...