Complete the code to get the type of the variable $value using gettype.
<?php $value = 123; $type = [1]($value); echo $type; ?>
The gettype() function returns the type of a variable as a string in PHP.
Complete the code to check if the variable $value is of type integer using gettype.
<?php $value = 10; if (gettype($value) === [1]) { echo 'Integer detected'; } ?>
The gettype() function returns 'integer' for integer values in PHP.
Fix the error in the code to correctly check the type of $value using gettype.
<?php $value = 3.14; if ([1]($value) == 'double') { echo 'Float detected'; } ?>
Only gettype() is a valid PHP function to get the type of a variable.
Fill both blanks to create a dictionary with words as keys and their lengths as values, only for words longer than 3 characters.
<?php $words = ['apple', 'cat', 'banana', 'dog']; $lengths = array(); foreach ($words as $word) { if (strlen($word) [1] 3) { $lengths[$word] = [2]($word); } } print_r($lengths); ?>
We check if the length of the word is greater than 3 using strlen($word) > 3 and then assign the length using strlen($word).
Fill all three blanks to create an associative array with uppercase keys and values only if the value is a string.
<?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); ?>
In the foreach loop, $key and $value are used to access keys and values. The condition checks if $value is a string, then adds it to $result with uppercase keys.