Skip to content
PHP Logo

When using if-else statements in PHP to check for the presence of a word in a text, what are some common pitfalls to avoid?

One common pitfall when using if-else statements in PHP to check for the presence of a word in a text is not accounting for case sensitivity. To avoid this issue, you can use the `stripos()` function instead of `strpos()`, as `stripos()` is case-insensitive. This ensures that the word can be found regardless of its case in the text.

php
$text = "This is a sample text";
$word = "sample";

if (stripos($text, $word) !== false) {
    echo "The word '$word' was found in the text.";
} else {
    echo "The word '$word' was not found in the text.";
}