Search code examples
phpstrposperiodpunctuation

Attempt at finding period in string with strpos not working


I'm trying to find if there's a period in a string with strpos, but for some reason it prints out "There's no period." every time I run the code. I'm not sure what I did wrong.

$text = "Hello.";

if (strpos($text, "." !== false)) {
echo "There's a period.";
}
else {
echo "There's no period.";
}

Expected result

There's a period.

Actual result

There's no period.

Solution

  • Your parenthesis are not matching correctly.

    With the way you have it right now, you are passing the result of "." !== false as the second argument to strpos.

    Change

    if (strpos($text, "." !== false)) {
    

    to

    if (strpos($text, ".") !== false) {