0
0
PHPprogramming~20 mins

Arithmetic operators in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Arithmetic Operator 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 using arithmetic operators?
Consider the following PHP code snippet. What will it output when run?
PHP
<?php
$a = 10;
$b = 3;
echo $a % $b;
?>
A3
BSyntaxError
C0
D1
Attempts:
2 left
💡 Hint
The % operator gives the remainder of division.
Predict Output
intermediate
2: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;
?>
A12.5
B27
C27.5
DSyntaxError
Attempts:
2 left
💡 Hint
Remember operator precedence: exponentiation before division and addition.
Predict Output
advanced
2: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;
?>
A8
B7
C6
DSyntaxError
Attempts:
2 left
💡 Hint
Remember post-increment returns the value before increment, pre-increment increments first.
Predict Output
advanced
2:00remaining
What error does this PHP code produce?
What error will this PHP code raise when executed?
PHP
<?php
$x = 10 / 0;
echo $x;
?>
ADivisionByZeroError
BWarning: Division by zero
CFatal error: Uncaught Exception
DNo error, outputs 0
Attempts:
2 left
💡 Hint
Division by zero is not allowed in PHP 7+ and throws an error.
🧠 Conceptual
expert
2: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]);
?>
A6
B5
C4
D7
Attempts:
2 left
💡 Hint
Count elements after adding and removing keys.