0
0
PHPprogramming~20 mins

Variable declaration with dollar sign in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PHP Variable Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
?>
A$hello
Bhello
Cworld
DError: Undefined variable
Attempts:
2 left
💡 Hint
Remember that $$a means the variable whose name is the value of $a.
Predict Output
intermediate
2: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";
?>
APHP is name
Bname is PHP
C$var is $name
DError: Undefined variable
Attempts:
2 left
💡 Hint
Variables inside double quotes are replaced by their values.
Predict Output
advanced
2: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;
?>
AParse error: syntax error, unexpected '1var'
BNotice: Undefined variable: 1var
CFatal error: Cannot use numeric variable name
Dtest
Attempts:
2 left
💡 Hint
Variable names in PHP cannot start with a number.
Predict Output
advanced
2: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;
?>
Aqux
BError: Undefined variable
Cbar
Dbaz
Attempts:
2 left
💡 Hint
Evaluate from right to left: $$$foo means ${$foo} then ${${$foo}}.
🧠 Conceptual
expert
2: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;
?>
A1
B4
C2
D3
Attempts:
2 left
💡 Hint
Count all variables with names starting with $var and those created dynamically.