0
0
PHPprogramming~10 mins

Type juggling 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 convert the string to an integer.

PHP
<?php
$value = "123";
$number = ([1])$value;
echo gettype($number); // outputs 'integer'
?>
Drag options to blanks, or click blank then click option'
Aint
Bstring
Cbool
Dfloat
Attempts:
3 left
💡 Hint
Common Mistakes
Using (string) instead of (int) will keep the value as a string.
Using (bool) will convert to true or false, not a number.
2fill in blank
medium

Complete the code to check if the string '10 apples' equals the number 10 using ==.

PHP
<?php
$result = ('10 apples' [1] 10);
echo $result ? 'true' : 'false';
?>
Drag options to blanks, or click blank then click option'
A!==
B===
C!=
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using === will return false because types differ.
Using != or !== will check for inequality, not equality.
3fill in blank
hard

Fix the error in the code to correctly add a string number and an integer.

PHP
<?php
$a = "5";
$b = 3;
$sum = $a [1] $b;
echo $sum; // should output 8
?>
Drag options to blanks, or click blank then click option'
A*
B.
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using . will concatenate strings, resulting in '53' instead of 8.
Using - or * will perform subtraction or multiplication, not addition.
4fill in blank
hard

Fill both blanks to create an array with keys as strings and values as integers using type juggling.

PHP
<?php
$items = ["1" => 100, "2" => 200];
foreach ($items as $key => $value) {
    echo (int) $key [1] $value . "\n";
}
?>
Drag options to blanks, or click blank then click option'
A+
B.
C-
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using . will join numbers as strings, not add them.
Using - or * will perform subtraction or multiplication, not addition.
5fill in blank
hard

Fill all three blanks to filter an array of strings, keeping only those that convert to integers greater than 10.

PHP
<?php
$values = ["5", "15", "abc", "20"];
$result = array_filter($values, function($val) {
    return (int) $val [1] 10;
});
print_r($result);
?>
Drag options to blanks, or click blank then click option'
A==
B>
C<
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using == will keep only values equal to 10, not greater.
Using < or != will filter incorrectly.