Challenge - 5 Problems
PHP String Interpolation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate1:00remaining
Output of simple variable interpolation
What is the output of this PHP code?
PHP
<?php $name = "Alice"; echo "Hello, $name!"; ?>
Attempts:
2 left
💡 Hint
Variables inside double quotes are replaced by their values.
✗ Incorrect
In PHP, variables inside double quotes are replaced by their values. So $name becomes Alice.
❓ Predict Output
intermediate1:30remaining
Output with array element interpolation
What will this PHP code output?
PHP
<?php $colors = ['red', 'green', 'blue']; echo "My favorite color is $colors[1]."; ?>
Attempts:
2 left
💡 Hint
Array elements inside double quotes need special syntax.
✗ Incorrect
PHP does not parse $colors[1] inside double quotes directly. It interpolates $colors to "Array" and treats [1]. as literal text.
❓ Predict Output
advanced2:00remaining
Correct syntax for array element interpolation
Which option outputs: My favorite color is green. ?
PHP
<?php $colors = ['red', 'green', 'blue']; // Fill in the echo statement ?>
Attempts:
2 left
💡 Hint
Use curly braces to clearly specify the array element inside double quotes.
✗ Incorrect
Using {$colors[1]} inside double quotes tells PHP to replace it with the array element at index 1.
❓ Predict Output
advanced1:30remaining
Output with object property interpolation
What is the output of this PHP code?
PHP
<?php class Person { public $name = 'Bob'; } $p = new Person(); echo "Hello, $p->name!"; ?>
Attempts:
2 left
💡 Hint
Object properties like $p->name can be interpolated directly inside double quotes.
✗ Incorrect
In PHP, object properties like $p->name are parsed and replaced by their values inside double quotes.
❓ Predict Output
expert2:30remaining
Correct interpolation of object property
Which option outputs: Hello, Bob! ?
PHP
<?php class Person { public $name = 'Bob'; } $p = new Person(); // Fill in the echo statement ?>
Attempts:
2 left
💡 Hint
Use curly braces around the entire object property expression.
✗ Incorrect
Using {$p->name} inside double quotes tells PHP to replace it with the object's property value.