How can one efficiently check for the existence of multiple words in a string using PHP, and what are the recommended approaches for this task?

To efficiently check for the existence of multiple words in a string using PHP, one can use the `strpos()` function in a loop to check for each word individually. Another approach is to use regular expressions with the `preg_match()` function to search for multiple words simultaneously. Both methods are effective for checking the presence of multiple words in a string.

// Method 1: Using strpos() in a loop
function checkWords($string, $words) {
    foreach ($words as $word) {
        if (strpos($string, $word) === false) {
            return false;
        }
    }
    return true;
}

// Method 2: Using preg_match() with regular expressions
function checkWordsRegex($string, $words) {
    $pattern = '/\b' . implode('\b|\b', $words) . '\b/';
    return preg_match($pattern, $string);
}

// Usage
$string = "This is a sample string";
$words = ['sample', 'string'];

if (checkWords($string, $words)) {
    echo "All words found in the string.";
} else {
    echo "Not all words found in the string.";
}

if (checkWordsRegex($string, $words)) {
    echo "All words found in the string.";
} else {
    echo "Not all words found in the string.";
}