0
0
PHPprogramming~10 mins

Gettype and typeof checks 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 get the type of the variable $value using gettype.

PHP
<?php
$value = 123;
$type = [1]($value);
echo $type;
?>
Drag options to blanks, or click blank then click option'
Atype_of
Btypeof
Cgettype
Dtype
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'typeof' which is not a PHP function.
Trying to use 'type' as a function.
2fill in blank
medium

Complete the code to check if the variable $value is of type integer using gettype.

PHP
<?php
$value = 10;
if (gettype($value) === [1]) {
    echo 'Integer detected';
}
?>
Drag options to blanks, or click blank then click option'
A'integer'
B'int'
C'number'
D'digit'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'int' instead of 'integer'.
Using 'number' which is not returned by gettype.
3fill in blank
hard

Fix the error in the code to correctly check the type of $value using gettype.

PHP
<?php
$value = 3.14;
if ([1]($value) == 'double') {
    echo 'Float detected';
}
?>
Drag options to blanks, or click blank then click option'
Atypeof
Btype
Ctype_of
Dgettype
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'typeof' which is not a PHP function.
Using 'type' or 'type_of' which do not exist.
4fill in blank
hard

Fill both blanks to create a dictionary with words as keys and their lengths as values, only for words longer than 3 characters.

PHP
<?php
$words = ['apple', 'cat', 'banana', 'dog'];
$lengths = array();
foreach ($words as $word) {
    if (strlen($word) [1] 3) {
        $lengths[$word] = [2]($word);
    }
}
print_r($lengths);
?>
Drag options to blanks, or click blank then click option'
A>
Bstrlen
C<
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' in the condition.
Using 'count' instead of 'strlen' for string length.
5fill in blank
hard

Fill all three blanks to create an associative array with uppercase keys and values only if the value is a string.

PHP
<?php
$data = ['name' => 'John', 'age' => 30, 'city' => 'Paris'];
$result = array();
foreach ($data as [1] => [2]) {
    if (gettype([2]) === 'string') {
        $result[strtoupper([1])] = [2];
    }
}
print_r($result);
?>
Drag options to blanks, or click blank then click option'
Akey
Bvalue
C$value
D$key
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names without '$'.
Mixing up key and value variable names.