0
0
PHPprogramming~20 mins

String interpolation in double quotes in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PHP String Interpolation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:00remaining
Output of simple variable interpolation
What is the output of this PHP code?
PHP
<?php
$name = "Alice";
echo "Hello, $name!";
?>
AHello, $name!
BHello, Alice!
CHello, !
DHello, name!
Attempts:
2 left
💡 Hint
Variables inside double quotes are replaced by their values.
Predict Output
intermediate
1: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].";
?>
AMy favorite color is Array[1].
BMy favorite color is green.
CMy favorite color is .
DMy favorite color is $colors[1].
Attempts:
2 left
💡 Hint
Array elements inside double quotes need special syntax.
Predict Output
advanced
2: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
?>
Aecho "My favorite color is $colors{1}.";
Becho "My favorite color is ${colors[1]}.";
Cecho "My favorite color is {$colors[1]}.";
Decho "My favorite color is $colors[1].";
Attempts:
2 left
💡 Hint
Use curly braces to clearly specify the array element inside double quotes.
Predict Output
advanced
1: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!";
?>
AHello, $p->name!
BHello, !
CHello, Object->name!
DHello, Bob!
Attempts:
2 left
💡 Hint
Object properties like $p->name can be interpolated directly inside double quotes.
Predict Output
expert
2: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
?>
Aecho "Hello, {$p->name}!";
Becho "Hello, $p->name!";
Cecho "Hello, ${p->name}!";
Decho "Hello, $p->{name}!";
Attempts:
2 left
💡 Hint
Use curly braces around the entire object property expression.