Challenge - 5 Problems
Arithmetic Operator 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 using arithmetic operators?
Consider the following PHP code snippet. What will it output when run?
PHP
<?php $a = 10; $b = 3; echo $a % $b; ?>
Attempts:
2 left
💡 Hint
The % operator gives the remainder of division.
✗ Incorrect
The expression 10 % 3 calculates the remainder when 10 is divided by 3, which is 1.
❓ Predict Output
intermediate2:00remaining
What is the output of this PHP code with mixed arithmetic?
What will this PHP code print?
PHP
<?php $x = 5; $y = 2; $result = $x ** $y + $x / $y; echo $result; ?>
Attempts:
2 left
💡 Hint
Remember operator precedence: exponentiation before division and addition.
✗ Incorrect
5 ** 2 = 25, 5 / 2 = 2.5, 25 + 2.5 = 27.5.
❓ Predict Output
advanced2:00remaining
What is the output of this PHP code with increment operators?
What will this PHP code output?
PHP
<?php $a = 3; $b = $a++ + ++$a; echo $b; ?>
Attempts:
2 left
💡 Hint
Remember post-increment returns the value before increment, pre-increment increments first.
✗ Incorrect
Initially, $a=3. $a++ returns 3 then $a becomes 4. ++$a increments $a to 5 then returns 5. So sum is 3 + 5 = 8.
❓ Predict Output
advanced2:00remaining
What error does this PHP code produce?
What error will this PHP code raise when executed?
PHP
<?php $x = 10 / 0; echo $x; ?>
Attempts:
2 left
💡 Hint
Division by zero is not allowed in PHP 7+ and throws an error.
✗ Incorrect
In PHP 7 and later, dividing by zero throws a DivisionByZeroError exception.
🧠 Conceptual
expert2:00remaining
How many items are in the resulting array after this PHP code runs?
What is the number of elements in the array $arr after running this code?
PHP
<?php $arr = []; for ($i = 0; $i < 5; $i++) { $arr[$i] = $i * 2; } $arr[5] = $arr[2] + $arr[4]; unset($arr[1]); ?>
Attempts:
2 left
💡 Hint
Count elements after adding and removing keys.
✗ Incorrect
Initially 5 elements (keys 0 to 4). Then key 5 is added (total 6). Then key 1 is removed (total 5).