Isset, empty, and is_null behavior in PHP - Time & Space Complexity
We want to understand how checking variables with isset, empty, and is_null affects the time it takes to run PHP code.
How does the time grow when we check many variables?
Analyze the time complexity of the following code snippet.
$values = [null, 0, '', 'hello', false, true];
foreach ($values as $val) {
if (isset($val)) {
echo "isset: yes\n";
}
if (empty($val)) {
echo "empty: yes\n";
}
if (is_null($val)) {
echo "is_null: yes\n";
}
}
This code checks each value in an array using isset, empty, and is_null.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each element in the array.
- How many times: Once for each element in the array.
Each check runs once per item, so the total checks grow as the number of items grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 30 checks (3 checks x 10 items) |
| 100 | 300 checks (3 checks x 100 items) |
| 1000 | 3000 checks (3 checks x 1000 items) |
Pattern observation: The number of operations grows directly with the number of items.
Time Complexity: O(n)
This means the time to run grows in a straight line as the number of values increases.
[X] Wrong: "isset, empty, and is_null take the same time no matter how many variables we check."
[OK] Correct: Each check runs for every item, so more items mean more total checks and more time.
Understanding how simple checks scale helps you write efficient code and explain your reasoning clearly in interviews.
"What if we replaced the foreach loop with nested loops over two arrays? How would the time complexity change?"
Practice
isset in PHP is true?Solution
Step 1: Understand
issetbehaviorissetreturns false if the variable does not exist or is set to null.Step 2: Analyze options
issetreturns false if the variable is not set or is null. correctly states this behavior:issetreturns false if the variable is not set or is null. Options A, C, and D are incorrect becauseissetreturns false for null, does not check emptiness or zero, and is not limited to strings.Final Answer:
issetreturns false if the variable is not set or is null. -> Option BQuick Check:
issetfalse if null or unset [OK]
- Thinking isset returns true for null variables
- Confusing isset with empty
- Assuming isset checks variable type
$var is empty in PHP?Solution
Step 1: Recall empty syntax
Theemptyfunction requires parentheses around the variable:empty($var).Step 2: Check each option
if (empty($var)) { ... } uses correct syntax. if (empty $var) { ... } misses parentheses, C uses a non-existent function, and B uses invalid syntax.Final Answer:
if (empty($var)) { ... } -> Option AQuick Check:
empty requires parentheses [OK]
- Omitting parentheses with empty
- Using is_empty instead of empty
- Using arrow operator incorrectly
$a = 0;
var_dump(isset($a));
var_dump(empty($a));
var_dump(is_null($a));
Solution
Step 1: Evaluate
$a is set and not null, soisset($a)when $a = 0isset($a)returns true.Step 2: Evaluate
Zero is considered empty in PHP, soempty($a)when $a = 0empty($a)returns true.Step 3: Evaluate
$a is integer zero, not null, sois_null($a)when $a = 0is_null($a)returns false.Final Answer:
bool(true)
bool(true)
bool(false) -> Option DQuick Check:
isset true, empty true for 0, is_null false [OK]
- Thinking isset returns false for 0
- Confusing empty with is_null
- Assuming 0 is null
$var = null;
if (empty($var)) {
echo 'Empty';
} else {
echo 'Not Empty';
}
Why does this code print "Empty"?
Solution
Step 1: Understand
emptybehavior with nullemptyreturns true if the variable is not set or its value is considered empty, including null.Step 2: Analyze the code output
Since$varis null,empty($var)returns true, so "Empty" is printed.Final Answer:
Becauseemptyreturns true for null values. -> Option CQuick Check:
empty true for null [OK]
- Thinking empty only checks if variable is set
- Assuming empty returns false for null
- Expecting error on null with empty
$x exists and is not null, but also want to treat the value 0 as valid (not empty). Which combination of PHP functions correctly achieves this?Solution
Step 1: Understand the requirement
We want to check if$xexists and is not null, but 0 should be valid (not treated as empty).Step 2: Analyze each option
if (isset($x) && !empty($x)) { ... } fails becauseempty(0)is true, so 0 would be excluded. if (isset($x) && $x !== null) { ... } checks if$xis set and not null, allowing 0. if (!is_null($x) && !empty($x)) { ... } excludes 0 because ofempty($x). if (!empty($x) || isset($x)) { ... } uses OR, which can be true even if$xis empty or null.Final Answer:
if (isset($x) && $x !== null) { ... } -> Option AQuick Check:
Use isset and strict null check to allow 0 [OK]
- Using empty excludes zero values
- Using OR instead of AND
- Confusing is_null with isset
Ready to prove it?
Pass the test and this pattern moves unknown
