A variable $word is set to “HELLO WORLD”, which of the following script returns in title case?

  1. echo ucwords($word)

  2. echo ucwords(strtolower($word)

  3. echo ucfirst($word)

  4. echo ucfirst(strtolower($word)


Correct Option: B
Explanation:

To solve this question, the user should have knowledge of PHP string functions.

The ucwords() function is used to convert the first letter of each word to uppercase in a given string. The ucfirst() function converts the first letter of a string to uppercase. The strtolower() function converts a string to lowercase.

Option A: echo ucwords($word) will return "HELLO WORLD", where the first letter of each word is capitalized. This option is incorrect, as the question asks for the title case.

Option B: echo ucwords(strtolower($word)) will return "Hello World". This option is correct as it converts the whole string to lowercase using strtolower() and then capitalizes the first letter of each word using ucwords().

Option C: echo ucfirst($word) will return "HELLO WORLD", where only the first letter of the first word is capitalized. This option is incorrect as it doesn't convert the string to title case.

Option D: echo ucfirst(strtolower($word)) will return "Hello world". This option is incorrect as it only capitalizes the first letter of the first word and converts the string to lowercase, but doesn't capitalize the first letter of each word.

Therefore, the correct answer is:

The Answer is: B. echo ucwords(strtolower($word))

Find more quizzes: