In PHP, we should use the str_word_count() function to count the number of words in a string.

Here’s its syntax: str_word_count ( $string , $returnVal, $chars )

To calculate the number of words in a string, use the function in the following way:

BY Best Interview Question ON 18 Aug 2020

Example

mystring = "This is a function";
print_r(str_word_count($mystring));