Challenge - 5 Problems
String Mastery in PHP
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of variable interpolation in strings
What is the output of the following PHP code?
PHP
<?php $name = "Alice"; echo 'Hello, $name!'; ?>
Attempts:
2 left
💡 Hint
Remember how single quotes treat variables inside strings in PHP.
✗ Incorrect
In PHP, single-quoted strings do not parse variables. So '$name' is treated as literal text.
❓ Predict Output
intermediate2:00remaining
Escape sequences in single vs double quotes
What will this PHP code output?
PHP
<?php echo "Line1\nLine2"; echo '\nLine3'; ?>
Attempts:
2 left
💡 Hint
Check how escape sequences like \n behave in single and double quotes.
✗ Incorrect
In double quotes, \n is interpreted as a newline character, so "Line1\nLine2" outputs two lines. In single quotes, escape sequences like \n are not interpreted, so '\nLine3' outputs a literal backslash and 'n' followed by 'Line3'. The combined output is:
Line1
Line2\nLine3
🔧 Debug
advanced2:00remaining
Why does this string concatenation fail?
Consider this PHP code snippet. Why does it cause an error?
PHP
<?php $name = "Bob"; echo 'Hello, ' . $name . '!'; echo 'He said, "Hi!"'; echo 'It\'s sunny'; ?>
Attempts:
2 left
💡 Hint
Look carefully at how quotes inside strings are escaped.
✗ Incorrect
There is no syntax error. Concatenation with '.' is correct. Double quotes inside single-quoted strings are literal and need no escaping. The third echo 'It\'s sunny' correctly escapes the single quote with a backslash, so it runs fine outputting: It's sunny.
❓ Predict Output
advanced2:00remaining
Output of complex variable interpolation
What is the output of this PHP code?
PHP
<?php $fruit = "apple"; echo "I have an {$fruit}s."; ?>
Attempts:
2 left
💡 Hint
Look at how curly braces help with variable interpolation.
✗ Incorrect
Curly braces tell PHP where the variable name ends, so {$fruit}s becomes 'apple' + 's'.
🧠 Conceptual
expert2:00remaining
Difference in performance between single and double quotes
Which statement about single and double quoted strings in PHP is TRUE?
Attempts:
2 left
💡 Hint
Think about what PHP does internally when parsing strings.
✗ Incorrect
Single quoted strings are faster because PHP treats them as literal text without parsing variables or most escape sequences (except \' and \\).