0
0
PHPprogramming~20 mins

Assignment and compound assignment in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Assignment Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of compound assignment with strings
What is the output of this PHP code?
PHP
<?php
$a = "Hello";
$a .= " World";
echo $a;
?>
AWorld
BHelloWorld
CHello
DHello World
Attempts:
2 left
💡 Hint
The .= operator adds the right string to the left string.
Predict Output
intermediate
2:00remaining
Result of compound assignment with numbers
What is the value of $x after running this PHP code?
PHP
<?php
$x = 10;
$x *= 3 + 2;
echo $x;
?>
A50
B15
C60
D30
Attempts:
2 left
💡 Hint
Remember operator precedence: multiplication and addition.
Predict Output
advanced
2:00remaining
Output of chained compound assignments
What is the output of this PHP code?
PHP
<?php
$a = 5;
$b = 10;
$c = 15;
$a += $b -= $c;
echo "$a $b $c";
?>
A0 -5 15
B20 -5 15
C20 25 15
D5 10 15
Attempts:
2 left
💡 Hint
Evaluate inner assignment first, then outer.
Predict Output
advanced
2:00remaining
Effect of compound assignment with arrays
What error or output does this PHP code produce?
PHP
<?php
$arr = [1, 2, 3];
$arr += [4, 5];
print_r($arr);
?>
AArray ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
BArray ( [0] => 1 [1] => 2 [2] => 3 )
CFatal error: Unsupported operand types
DArray ( [0] => 1 [1] => 2 [2] => 3 [4] => 5 )
Attempts:
2 left
💡 Hint
The += operator for arrays adds elements with keys that do not exist in the left array.
Predict Output
expert
2:00remaining
Final value after mixed compound assignments
What is the final value of $result after running this PHP code?
PHP
<?php
$result = 2;
$result *= 3;
$result += 4;
$result /= 2;
$result -= 1;
echo $result;
?>
A3
B5
C4
D6
Attempts:
2 left
💡 Hint
Calculate step by step following the order of operations.