Complete the code to change the variable type to integer using settype.
<?php $value = "123"; settype($value, [1]); echo gettype($value); // outputs 'integer' ?>
The settype function changes the type of a variable. To convert to integer, use "integer" as the type.
Complete the code to convert the variable to boolean using settype.
<?php $value = 0; settype($value, [1]); echo gettype($value); // outputs 'boolean' ?>
To convert a variable to boolean, use "bool" as the type in settype.
Fix the error in the code to correctly convert the variable to float using settype.
<?php $value = "3.14"; settype($value, [1]); echo gettype($value); // outputs 'double' ?>
In PHP, settype accepts "float" (also "double" or "real") to convert to floating point number. The correct choice here is "float".
Fill both blanks to create a dictionary with word lengths only for words longer than 3 characters.
<?php $words = ["apple", "cat", "banana", "dog"]; $lengths = []; foreach ($words as $word) { if (strlen($word) [1] 3) { $lengths[$word] = [2]($word); } } print_r($lengths); ?>
The condition checks if the word length is greater than 3 using strlen($word) > 3. Then it stores the length using strlen($word).
Fill all three blanks to create an associative array with uppercase keys and values only if value is positive.
<?php $data = ["a" => 1, "b" => -2, "c" => 3]; $result = []; foreach ($data as $key => $value) { if ($value [1] 0) { $result[[2]] = [3]; } } print_r($result); ?>
The condition checks if the value is greater than zero. The key is converted to uppercase using strtoupper($key). The value is assigned as is.