Challenge - 5 Problems
PHP Variable 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 with variable variables?
Consider the following PHP code snippet. What will it output?
PHP
<?php $a = 'hello'; $hello = 'world'; echo $$a; ?>
Attempts:
2 left
💡 Hint
Remember that $$a means the variable whose name is the value of $a.
✗ Incorrect
The variable $a contains the string 'hello'. So $$a means $hello, which contains 'world'. Thus, echo $$a outputs 'world'.
❓ Predict Output
intermediate2:00remaining
What will this PHP code output?
Analyze the following PHP code and select the output it produces.
PHP
<?php $var = 'name'; $name = 'PHP'; echo "$var is $name"; ?>
Attempts:
2 left
💡 Hint
Variables inside double quotes are replaced by their values.
✗ Incorrect
Inside double quotes, $var and $name are replaced by their values 'name' and 'PHP' respectively. So the output is 'name is PHP'.
❓ Predict Output
advanced2:00remaining
What error does this PHP code raise?
Examine the code below. What error will it produce when run?
PHP
<?php $1var = 'test'; echo $1var; ?>
Attempts:
2 left
💡 Hint
Variable names in PHP cannot start with a number.
✗ Incorrect
PHP variable names must start with a letter or underscore. Starting with a number causes a syntax error.
❓ Predict Output
advanced2:00remaining
What is the output of this PHP code using variable variables and concatenation?
What will this PHP code print?
PHP
<?php $foo = 'bar'; $bar = 'baz'; $baz = 'qux'; echo $$$foo; ?>
Attempts:
2 left
💡 Hint
Evaluate from right to left: $$$foo means ${$foo} then ${${$foo}}.
✗ Incorrect
First, $foo = 'bar', so $$foo = $bar = 'baz'. Then $$$foo = ${$bar} = $baz = 'qux'. So echo $$$foo outputs 'qux'.
🧠 Conceptual
expert2:00remaining
How many variables are declared and accessible after this PHP code runs?
Consider this PHP code snippet. How many distinct variables are declared and accessible after execution?
PHP
<?php
${'var'.'1'} = 10;
$var2 = 20;
$var3 = ${'var'.'2'} + 5;
?>Attempts:
2 left
💡 Hint
Count all variables with names starting with $var and those created dynamically.
✗ Incorrect
Variables declared: $var1 (via ${'var'.'1'}), $var2, and $var3. So total 3 variables accessible.