Bird
Raised Fist0
PHPprogramming~20 mins

Case conversion functions in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Case Conversion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of strtolower and strtoupper
What is the output of this PHP code?
PHP
<?php
$text = "Hello World!";
echo strtolower($text) . " - " . strtoupper($text);
?>
Ahello world! - HELLO WORLD!
BHELLO WORLD! - hello world!
CHello World! - Hello World!
Dhello world! - hello world!
Attempts:
2 left
💡 Hint
strtolower converts all letters to lowercase, strtoupper converts all letters to uppercase.
❓ Predict Output
intermediate
2:00remaining
Output of ucfirst and lcfirst
What is the output of this PHP code?
PHP
<?php
$text = "hello WORLD!";
echo ucfirst($text) . " - " . lcfirst(strtoupper($text));
?>
AHello WORLD! - Hello WORLD!
Bhello WORLD! - Hello WORLD!
CHello WORLD! - hELLO WORLD!
Dhello WORLD! - hELLO world!
Attempts:
2 left
💡 Hint
ucfirst makes first letter uppercase, lcfirst makes first letter lowercase.
❓ Predict Output
advanced
2:00remaining
Output of mb_convert_case with mixed UTF-8 string
What is the output of this PHP code using mb_convert_case with UTF-8 encoding?
PHP
<?php
$text = "mÜnchEn ist schön";
echo mb_convert_case($text, MB_CASE_TITLE, "UTF-8");
?>
AmÜnchEn ist schön
BMünchen Ist Schön
CMÜNCHEN IST SCHÖN
DMünchEn Ist Schön
Attempts:
2 left
💡 Hint
MB_CASE_TITLE capitalizes the first letter of each word correctly for UTF-8 strings.
❓ Predict Output
advanced
2:00remaining
Output of strcasecmp function
What is the output of this PHP code?
PHP
<?php
$a = "Hello";
$b = "hello";
$result = strcasecmp($a, $b);
echo $result > 0 ? "A" : ($result < 0 ? "B" : "C");
?>
AA
BError
CB
DC
Attempts:
2 left
💡 Hint
strcasecmp compares strings ignoring case and returns 0 if equal.
🧠 Conceptual
expert
3:00remaining
Why use mb_strtolower over strtolower?
Why should you use mb_strtolower instead of strtolower when working with UTF-8 strings in PHP?
Amb_strtolower correctly converts multibyte UTF-8 characters to lowercase, while strtolower only works reliably with ASCII characters.
Bmb_strtolower is faster than strtolower for all strings.
Cstrtolower converts strings to uppercase, mb_strtolower converts to lowercase.
Dmb_strtolower only works with ASCII, strtolower works with UTF-8.
Attempts:
2 left
💡 Hint
Think about characters like accented letters or non-English alphabets.

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