Bird
Raised Fist0
PHPprogramming~10 mins

Case conversion functions in PHP - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Case conversion functions
Start with a string
↓
Choose case function
↓
Apply function to string
↓
Get converted string
↓
Use or print result
↓
End
Start with a string, pick a case conversion function, apply it, then get the converted string to use or print.
Execution Sample
PHP
<?php
$str = "hello world";
echo strtoupper($str);
?>
This code converts the string 'hello world' to uppercase and prints it.
Execution Table
StepVariableFunction CalledInput ValueOutput ValueAction
1$strNone"hello world""hello world"Initialize string variable
2$strstrtoupper"hello world""HELLO WORLD"Convert string to uppercase
3Outputecho"HELLO WORLD"HELLO WORLDPrint the uppercase string
4----End of script
💡 Script ends after printing the converted string.
Variable Tracker
VariableStartAfter strtoupperFinal
$str"hello world""hello world""hello world"
OutputN/A"HELLO WORLD""HELLO WORLD"
Key Moments - 2 Insights
Why does $str not change after strtoupper()?
strtoupper() returns a new string and does not modify $str itself, as shown in execution_table step 2 where input is unchanged but output is new.
What happens if we use ucfirst() on 'hello world'?
ucfirst() changes only the first character of the string to uppercase, so output would be 'Hello world', not all uppercase.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output value after step 2?
A"Hello World"
B"hello world"
C"HELLO WORLD"
D"HELLO world"
💡 Hint
Check the 'Output Value' column in execution_table row 2.
At which step is the string printed to the screen?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look for the 'Action' column mentioning 'Print' in execution_table.
If we replace strtoupper() with ucfirst(), what would be the output at step 3?
A"Hello world"
B"HELLO WORLD"
C"hello world"
D"HELLO world"
💡 Hint
ucfirst() capitalizes only the first letter; see key_moments explanation.
Concept Snapshot
PHP case conversion functions:
- strtolower($str): all lowercase
- strtoupper($str): all uppercase
- ucfirst($str): uppercase first char only
- ucwords($str): uppercase first char of each word
Functions return new strings; original unchanged.
Full Transcript
This visual trace shows how PHP case conversion functions work. We start with a string variable $str set to 'hello world'. Then we apply strtoupper($str) which returns a new string 'HELLO WORLD' without changing the original. The new string is printed using echo. Key points: these functions do not modify the original string but return a new one. ucfirst() only changes the first character to uppercase. The execution table tracks each step, variable values, and actions clearly.

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

  1. Step 1: Understand the function purpose

    strtoupper() converts all characters in a string to uppercase letters.
  2. 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.
  3. Final Answer:

    strtoupper() -> Option A
  4. 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

  1. Step 1: Identify correct function call syntax

    In PHP, functions are called with parentheses enclosing arguments, like functionName(argument);.
  2. Step 2: Check each option

    strtolower($str); uses correct syntax: strtolower($str);. Others have syntax errors or invalid usage.
  3. Final Answer:

    strtolower($str); -> Option C
  4. 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

  1. Step 1: Understand ucfirst() behavior

    ucfirst() capitalizes only the first character of the entire string, leaving the rest unchanged.
  2. Step 2: Apply to given string

    Input is "john doe". After ucfirst(), it becomes "John doe" (only first letter capitalized).
  3. Final Answer:

    John doe -> Option A
  4. 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

  1. Step 1: Identify function behavior

    ucfirst() capitalizes only the first letter of the entire string, not each word.
  2. Step 2: Choose function to capitalize each word

    ucwords() capitalizes the first letter of every word in the string.
  3. Final Answer:

    Replace ucfirst() with ucwords() -> Option B
  4. 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

  1. Step 1: Normalize string to lowercase

    Use strtolower() to convert all letters to lowercase first, so mixed case is fixed.
  2. Step 2: Capitalize first letter of each word

    Apply ucwords() to capitalize the first letter of every word in the now lowercase string.
  3. Final Answer:

    $output = ucwords(strtolower($input)); -> Option D
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard