0
0
PHPprogramming~10 mins

Settype for changing types 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 change the variable type to integer using settype.

PHP
<?php
$value = "123";
settype($value, [1]);
echo gettype($value); // outputs 'integer'
?>
Drag options to blanks, or click blank then click option'
A"string"
B"int"
C"bool"
D"integer"
Attempts:
3 left
💡 Hint
Common Mistakes
Using "int" instead of "integer" (both work but here only "integer" is correct).
Using "string" which does not convert to integer.
Using "bool" which converts to boolean, not integer.
2fill in blank
medium

Complete the code to convert the variable to boolean using settype.

PHP
<?php
$value = 0;
settype($value, [1]);
echo gettype($value); // outputs 'boolean'
?>
Drag options to blanks, or click blank then click option'
A"int"
B"bool"
C"string"
D"float"
Attempts:
3 left
💡 Hint
Common Mistakes
Using "boolean" instead of "bool" (both work but here only "bool" is correct).
Using "int" which converts to integer, not boolean.
Using "string" which converts to string.
3fill in blank
hard

Fix the error in the code to correctly convert the variable to float using settype.

PHP
<?php
$value = "3.14";
settype($value, [1]);
echo gettype($value); // outputs 'double'
?>
Drag options to blanks, or click blank then click option'
A"float"
B"decimal"
C"double"
D"real"
Attempts:
3 left
💡 Hint
Common Mistakes
Using "double" instead of "float" (both work but here only "float" is correct).
Using "decimal" which is not a valid type.
Using "real" instead of "float" (both work but here only "float" is correct).
4fill in blank
hard

Fill both blanks to create a dictionary with word lengths only for words longer than 3 characters.

PHP
<?php
$words = ["apple", "cat", "banana", "dog"];
$lengths = [];
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 '>' causing wrong filtering.
Using 'count' which works on arrays, not strings.
Using 'strlen' incorrectly in the condition.
5fill in blank
hard

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

PHP
<?php
$data = ["a" => 1, "b" => -2, "c" => 3];
$result = [];
foreach ($data as $key => $value) {
    if ($value [1] 0) {
        $result[[2]] = [3];
    }
}
print_r($result);
?>
Drag options to blanks, or click blank then click option'
A>
B$value
C$key
Dstrtoupper($key)
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' in the condition.
Using '$key' instead of 'strtoupper($key)' for keys.
Swapping key and value in the assignment.