What function can be used to count how many times a string appears in a variable in PHP?

To count how many times a string appears in a variable in PHP, you can use the `substr_count()` function. This function takes two parameters: the string to search for and the variable containing the string to search within. It returns the number of times the specified string appears in the variable. This can be useful for tasks such as counting occurrences of a specific word in a text.

$variable = "This is a sample string with sample words.";
$searchString = "sample";
$count = substr_count($variable, $searchString);
echo "The string '$searchString' appears $count times in the variable.";