PHP Check if String Contains Word With Example

Hello Friends Today, through this tutorial, I will tell you How do you check if string contains word using PHP With Example? In PHP, you can check if a string contains a specific word or substring using the `strpos()` function or the `stripos()` function if you want to perform a case-insensitive search. Here’s how you can do it:

Using `strpos()`:

<?php
$string = "This is a sample string.";
$word = "sample";
if (strpos($string, $word) !== false) {
echo "The string contains the word '$word'.";
} else {
echo "The string does not contain the word '$word'.";
}
?>

Using `stripos()` for case-insensitive search:

<?php
$string = "This is a sample string.";
$word = "Sample";
if (stripos($string, $word) !== false) {
echo "The string contains the word '$word' (case-insensitive).";
} else {
echo "The string does not contain the word '$word' (case-insensitive).";
}
?>

In both cases, `strpos()` and `stripos()` return the position of the first occurrence of the substring in the string if found, and `false` otherwise. The `!== false` comparison ensures that you’re checking for a non-boolean false value, as `strpos()` and `stripos()` may return 0 if the substring is found at the beginning of the string, which would be considered falsy if you use a loose comparison (`!=`).