0
0
PHPprogramming~10 mins

Arithmetic operators in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to add two numbers and print the result.

PHP
<?php
$a = 5;
$b = 3;
$result = $a [1] $b;
echo $result;
?>
Drag options to blanks, or click blank then click option'
A/
B-
C+
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using minus - instead of plus +.
2fill in blank
medium

Complete the code to multiply two numbers and print the result.

PHP
<?php
$x = 7;
$y = 4;
$product = $x [1] $y;
echo $product;
?>
Drag options to blanks, or click blank then click option'
A*
B-
C+
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using plus + instead of multiplication *.
3fill in blank
hard

Fix the error in the code to correctly divide two numbers and print the result.

PHP
<?php
$num1 = 10;
$num2 = 2;
$division = $num1 [1] $num2;
echo $division;
?>
Drag options to blanks, or click blank then click option'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using minus - or multiplication * instead of division /.
4fill in blank
hard

Fill both blanks to create a dictionary of squares for numbers greater than 3.

PHP
<?php
$numbers = [1, 2, 3, 4, 5];
$squares = [];
foreach ($numbers as $num) {
    if ($num [1] 3) {
        $squares[$num] = $num [2] 2;
    }
}
print_r($squares);
?>
Drag options to blanks, or click blank then click option'
A>
B<
C**
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using less than < instead of greater than >.
Using multiplication * instead of power ** for squaring.
5fill in blank
hard

Fill all three blanks to create an array of numbers and filter those divisible by 2.

PHP
<?php
$numbers = range(1, 10);
$evens = [];
foreach ($numbers as [1]) {
    if ([2] % 2 [3] 0) {
        $evens[] = [2];
    }
}
print_r($evens);
?>
Drag options to blanks, or click blank then click option'
A$num
Bnum
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable without $ sign.
Using not equal != instead of equal ==.