📖 Scenario: You are working on a simple text processing tool that changes the case of words. This is useful when you want to standardize names or titles in a list.
🎯 Goal: Build a PHP script that converts a list of words to uppercase, lowercase, and capitalized forms using PHP's case conversion functions.
📋 What You'll Learn
Create an array of words with exact values
Create a variable to hold the number of words
Use a for loop to convert each word to uppercase and store in a new array
Print the original array and the uppercase array
💡 Why This Matters
🌍 Real World
Changing the case of text is common in data cleaning, formatting user input, or preparing text for display.
💼 Career
Many programming jobs require manipulating strings and arrays, especially for web development and data processing.
Progress0 / 4 steps
1
Create the initial array of words
Create an array called words with these exact values: 'apple', 'Banana', 'Cherry', 'date', 'Elderberry'.
PHP
Hint
Use $words = [ ... ]; to create the array with the exact words.
2
Create a variable for the number of words
Create a variable called count and set it to the number of elements in the $words array using the count() function.
PHP
Hint
Use count($words) to get the number of words.
3
Convert words to uppercase using a for loop
Create an empty array called upperWords. Use a for loop with the variable i from 0 to $count - 1. Inside the loop, convert $words[i] to uppercase using strtoupper() and store it in upperWords[i].
PHP
Hint
Use strtoupper() to convert each word to uppercase inside the loop.
4
Print the original and uppercase arrays
Use print_r() to print the $words array with the label "Original words:". Then print the $upperWords array with the label "Uppercase words:".
PHP
Hint
Use print_r() to display arrays with labels.
Practice
(1/5)
1. Which PHP function converts all letters in a string to uppercase? $text = "hello world";
easy
A. strtoupper()
B. strtolower()
C. ucfirst()
D. ucwords()
Solution
Step 1: Understand the function purpose
strtoupper() converts all characters in a string to uppercase letters.
Step 2: Compare with other functions
strtolower() makes all letters lowercase, ucfirst() capitalizes only the first letter, and ucwords() capitalizes the first letter of each word.
Final Answer:
strtoupper() -> Option A
Quick Check:
Uppercase function = strtoupper() [OK]
Hint: All uppercase letters? Use strtoupper() [OK]
Common Mistakes:
Confusing strtoupper() with strtolower()
Using ucfirst() to uppercase whole string
Using ucwords() for full uppercase
2. Which of the following is the correct syntax to convert a string to lowercase in PHP? $str = "HELLO";
easy
A. strtolower->$str();
B. strtolower $str;
C. strtolower($str);
D. strtolower{$str};
Solution
Step 1: Identify correct function call syntax
In PHP, functions are called with parentheses enclosing arguments, like functionName(argument);.
Step 2: Check each option
strtolower($str); uses correct syntax: strtolower($str);. Others have syntax errors or invalid usage.
Final Answer:
strtolower($str); -> Option C
Quick Check:
Function call syntax = strtolower($str); [OK]
Hint: Function calls need parentheses around arguments [OK]
Common Mistakes:
Missing parentheses in function call
Using arrow or braces incorrectly
Confusing function call with variable access
3. What is the output of the following PHP code?
$name = "john doe"; echo ucfirst($name);
medium
A. John doe
B. john Doe
C. JOHN DOE
D. john doe
Solution
Step 1: Understand ucfirst() behavior
ucfirst() capitalizes only the first character of the entire string, leaving the rest unchanged.
Step 2: Apply to given string
Input is "john doe". After ucfirst(), it becomes "John doe" (only first letter capitalized).
Final Answer:
John doe -> Option A
Quick Check:
ucfirst("john doe") = "John doe" [OK]
Hint: ucfirst() capitalizes only first letter of whole string [OK]
Common Mistakes:
Expecting all words capitalized
Confusing ucfirst() with ucwords()
Assuming it converts whole string to uppercase
4. The following PHP code is intended to capitalize the first letter of each word in a string, but it does not work as expected:
$text = "hello world"; echo ucfirst($text);
What is the correct fix?
medium
A. Replace ucfirst() with strtoupper()
B. Replace ucfirst() with ucwords()
C. Replace ucfirst() with strtolower()
D. No fix needed, code is correct
Solution
Step 1: Identify function behavior
ucfirst() capitalizes only the first letter of the entire string, not each word.
Step 2: Choose function to capitalize each word
ucwords() capitalizes the first letter of every word in the string.
Final Answer:
Replace ucfirst() with ucwords() -> Option B
Quick Check:
Capitalize each word = ucwords() [OK]
Hint: Use ucwords() to capitalize first letter of each word [OK]
Common Mistakes:
Using ucfirst() instead of ucwords()
Using strtoupper() which uppercases all letters
Thinking code is already correct
5. You have a string with mixed case: $input = "hElLo WoRLd";. You want to convert it so that only the first letter of each word is uppercase and the rest are lowercase. Which PHP code achieves this?
hard
A. $output = ucfirst(strtolower($input));
B. $output = strtoupper(ucwords($input));
C. $output = strtolower(ucfirst($input));
D. $output = ucwords(strtolower($input));
Solution
Step 1: Normalize string to lowercase
Use strtolower() to convert all letters to lowercase first, so mixed case is fixed.
Step 2: Capitalize first letter of each word
Apply ucwords() to capitalize the first letter of every word in the now lowercase string.
Final Answer:
$output = ucwords(strtolower($input)); -> Option D
Quick Check:
Lowercase then ucwords() = desired format [OK]
Hint: Lowercase first, then ucwords() for title case [OK]
Common Mistakes:
Applying ucwords() before strtolower()
Using strtoupper() which uppercases all letters
Using ucfirst() which only capitalizes first letter of string
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.