Complete the code to check if the variable $var is set.
<?php $var = 5; if ([1]($var)) { echo "Variable is set."; } else { echo "Variable is not set."; } ?>
The isset() function checks if a variable is set and is not null.
Complete the code to check if the variable $var is empty.
<?php $var = 0; if ([1]($var)) { echo "Variable is empty."; } else { echo "Variable is not empty."; } ?>
The empty() function checks if a variable is empty. It returns true for values like 0, '', null, false, and empty arrays.
Fix the error in the code to correctly check if $var is null.
<?php $var = null; if ([1]($var)) { echo "Variable is null."; } else { echo "Variable is not null."; } ?>
The is_null() function checks if a variable is exactly null.
Fill both blanks to include only words with length greater than 3 in the associative array.
<?php $words = ['apple', 'cat', 'dog', 'banana']; $result = []; foreach ($words as $word) { if ([1]($word) [2] 3) { $result[$word] = strlen($word); } } print_r($result); ?>
Use strlen() to get the length of the word and check if it is greater than 3.
Fill all three blanks to create an array filtering code that keeps only non-empty, set, and non-null values.
<?php $values = [0, null, '', 'hello', false, 42]; $result = []; foreach ($values as $val) { if ( && [2]($val) && ) { $result[] = $val; } } print_r($result); ?>
The code filters values that are not empty and are set. The empty() function checks for empty values, isset() checks if variable is set, and is_null() can be used to check for null values if needed.