0
0
PHPprogramming~10 mins

Integer type and behavior 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 declare an integer variable with value 10.

PHP
<?php
$number = [1];
echo $number;
?>
Drag options to blanks, or click blank then click option'
A10
B"10"
C10.0
D'10'
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around the number makes it a string, not an integer.
Using a decimal point makes it a float, not an integer.
2fill in blank
medium

Complete the code to check if a variable is an integer using a built-in function.

PHP
<?php
$var = 5;
if ([1]($var)) {
    echo "Integer";
} else {
    echo "Not integer";
}
?>
Drag options to blanks, or click blank then click option'
Ais_int
Bis_string
Cis_float
Dis_numeric
Attempts:
3 left
💡 Hint
Common Mistakes
Using is_string or is_float instead of is_int.
Using is_numeric which returns true for floats and numeric strings too.
3fill in blank
hard

Fix the error in the code to correctly add two integers and print the result.

PHP
<?php
$a = 7;
$b = 3;
$sum = $a [1] $b;
echo $sum;
?>
Drag options to blanks, or click blank then click option'
A-
B+
C.
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using the dot operator . which concatenates strings instead of adding numbers.
Using subtraction or multiplication operators instead of addition.
4fill in blank
hard

Fill both blanks to create an array of squares for numbers 1 to 5 using a loop.

PHP
<?php
$squares = [];
for ($i = 1; $i [1] 5; $i++) {
    $squares[] = $i [2] $i;
}
print_r($squares);
?>
Drag options to blanks, or click blank then click option'
A<=
B*
C+
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of <= causes the loop to stop before 5.
Using addition instead of multiplication for squares.
5fill in blank
hard

Fill all three blanks to create an associative array with keys as numbers and values as their cubes, only for numbers greater than 2.

PHP
<?php
$cubes = [];
foreach (range(1, 5) as [1]) {
    if ([2] > 2) {
        $cubes[[3]] = [2] * [2] * [2];
    }
}
print_r($cubes);
?>
Drag options to blanks, or click blank then click option'
A$num
D$val
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in the loop and condition causes errors.
Using undefined variables like $val.