Look at this PHP code. What will it print?
<?php $value = null; if (is_null($value)) { echo 'Yes'; } else { echo 'No'; } ?>
Check what is_null() does when the variable is null.
The function is_null() returns true if the variable is null. So it prints 'Yes'.
What will this PHP code print?
<?php var_dump(null == ''); var_dump(null === ''); ?>
Remember that == checks value equality with type juggling, === checks type and value.
Using ==, null and empty string are not considered equal (false). Using ===, types differ so false.
Find the reason for the warning in this PHP code:
<?php $value = null; echo strlen($value); ?>
Check what type strlen() expects as input.
strlen() expects a string. Passing null causes a warning because null is not a string.
Choose the best description of the null coalescing operator ?? in PHP.
Think about how to provide a default value when a variable might be null.
The operator ?? returns the left operand if it is not null; otherwise, it returns the right operand.
What will this PHP code print?
<?php $array = ['a' => null, 'b' => 0, 'c' => false]; $count = 0; foreach ($array as $key => $value) { if ($value == null) { $count++; } } echo $count; ?>
Remember that == null is true for null and false, but not for 0 in PHP.
In PHP, == null is true for null and false, but not for 0. So keys 'a' and 'c' match, count is 2.