Challenge - 5 Problems
PHP String Concatenation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this PHP code using concatenation?
Consider the following PHP code snippet. What will it print?
PHP
<?php $a = "Hello"; $b = "World"; echo $a . " " . $b . "!"; ?>
Attempts:
2 left
💡 Hint
Remember the dot (.) operator joins strings exactly as written.
✗ Incorrect
The dot operator concatenates strings exactly. Here, $a and $b are joined with a space and an exclamation mark, producing 'Hello World!'.
❓ Predict Output
intermediate2:00remaining
What does this PHP code output with mixed concatenation and addition?
Look at this PHP code. What will it output?
PHP
<?php $x = 5; $y = "10"; echo $x + $y . " apples"; ?>
Attempts:
2 left
💡 Hint
PHP converts strings to numbers when using + operator.
✗ Incorrect
The + operator adds numbers, so $y is converted from "10" to 10. Then 5 + 10 = 15. The dot concatenates '15' with ' apples'.
🔧 Debug
advanced2:00remaining
Which option causes a syntax error in PHP string concatenation?
Identify which code snippet will cause a syntax error when run.
Attempts:
2 left
💡 Hint
Check the operator symbols carefully.
✗ Incorrect
Option A uses '.+' which is not a valid operator in PHP, causing a syntax error.
❓ Predict Output
advanced2:00remaining
What is the output of this PHP code with concatenation assignment?
What will this PHP code print?
PHP
<?php $str = "Good"; $str .= " Morning"; $str .= "!"; echo $str; ?>
Attempts:
2 left
💡 Hint
The '.=' operator adds to the existing string.
✗ Incorrect
The '.=' operator appends strings. So $str becomes 'Good Morning!' after both concatenations.
🧠 Conceptual
expert2:00remaining
How many items are in the resulting array after this PHP code runs?
What is the count of elements in the array after this code executes?
PHP
<?php $arr = []; $arr['a'] = 'Hello'; $arr['b'] = 'World'; $arr['a'] .= ' PHP'; ?>
Attempts:
2 left
💡 Hint
Appending to an existing key does not add new elements.
✗ Incorrect
The array has keys 'a' and 'b'. The '.=' operator appends to the value at 'a' but does not add new keys. So total elements remain 2.