isset() checks if a variable is set and is not null. It returns true if the variable exists and has a value other than null, otherwise false.
Click to reveal answer
beginner
How does empty() behave with different variable values?
empty() returns true if a variable is not set or its value is considered "empty". This includes 0, "0", "" (empty string), null, false, empty arrays, and unset variables.
Click to reveal answer
intermediate
What is the difference between is_null() and isset()?
is_null() returns true only if the variable's value is exactly null. isset() returns false if the variable is null or not set at all.
Click to reveal answer
beginner
What will empty() return for a variable set to false?
empty() will return true because false is considered an empty value in PHP.
Click to reveal answer
intermediate
If a variable is not declared at all, what will isset() and empty() return?
isset() will return false because the variable does not exist. empty() will return true because the variable is considered empty if not set.
Click to reveal answer
What does isset($var) return if $var = null;?
Atrue
BThrows an error
Cnull
Dfalse
✗ Incorrect
isset() returns false if the variable is null.
Which of these values will empty() consider as empty?
A1
B"0"
C"Hello"
D42
✗ Incorrect
empty() treats the string "0" as empty.
What does is_null($var) return if $var is not set?
Anull
Bfalse
CThrows a warning
Dtrue
✗ Incorrect
is_null() on an unset variable throws a warning about undefined variable.
If $var = false;, what does isset($var) return?
Atrue
Bfalse
Cnull
DDepends on PHP version
✗ Incorrect
isset() returns true because $var is set and not null.
Which function returns true for an empty array?
Aempty()
Bis_null()
Cisset()
DNone of the above
✗ Incorrect
empty() returns true for empty arrays.
Explain how isset(), empty(), and is_null() differ in checking variable states in PHP.
Think about what each function considers as 'set' or 'empty'.
You got /5 concepts.
Describe what happens when you use empty() on a variable that is not declared.
Consider how empty handles unset variables compared to isset.
You got /3 concepts.
Practice
(1/5)
1. Which of the following statements about isset in PHP is true?
easy
A. isset returns true even if the variable is null.
B. isset returns false if the variable is not set or is null.
C. isset checks if a variable is empty or zero.
D. isset returns true only if the variable is a string.
Solution
Step 1: Understand isset behavior
isset returns false if the variable does not exist or is set to null.
Step 2: Analyze options
isset returns false if the variable is not set or is null. correctly states this behavior: isset returns false if the variable is not set or is null. Options A, C, and D are incorrect because isset returns false for null, does not check emptiness or zero, and is not limited to strings.
Final Answer:
isset returns false if the variable is not set or is null. -> Option B
Quick Check:
isset false if null or unset [OK]
Hint: Remember: isset false if variable missing or null [OK]
Common Mistakes:
Thinking isset returns true for null variables
Confusing isset with empty
Assuming isset checks variable type
2. Which of the following is the correct syntax to check if a variable $var is empty in PHP?
easy
A. if (empty($var)) { ... }
B. if (empty->$var) { ... }
C. if (is_empty($var)) { ... }
D. if (empty $var) { ... }
Solution
Step 1: Recall empty syntax
The empty function 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 A
Quick Check:
empty requires parentheses [OK]
Hint: Use parentheses with empty: empty($var) [OK]
Common Mistakes:
Omitting parentheses with empty
Using is_empty instead of empty
Using arrow operator incorrectly
3. What will be the output of the following PHP code?
empty returns true if the variable is not set or its value is considered empty, including null.
Step 2: Analyze the code output
Since $var is null, empty($var) returns true, so "Empty" is printed.
Final Answer:
Because empty returns true for null values. -> Option C
Quick Check:
empty true for null [OK]
Hint: empty true for null or unset variables [OK]
Common Mistakes:
Thinking empty only checks if variable is set
Assuming empty returns false for null
Expecting error on null with empty
5. You want to check if a variable $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?
hard
A. if (isset($x) && $x !== null) { ... }
B. if (isset($x) && !empty($x)) { ... }
C. if (!is_null($x) && !empty($x)) { ... }
D. if (!empty($x) || isset($x)) { ... }
Solution
Step 1: Understand the requirement
We want to check if $x exists and is not null, but 0 should be valid (not treated as empty).
Step 2: Analyze each option
if (isset($x) && !empty($x)) { ... } fails because empty(0) is true, so 0 would be excluded. if (isset($x) && $x !== null) { ... } checks if $x is set and not null, allowing 0. if (!is_null($x) && !empty($x)) { ... } excludes 0 because of empty($x). if (!empty($x) || isset($x)) { ... } uses OR, which can be true even if $x is empty or null.
Final Answer:
if (isset($x) && $x !== null) { ... } -> Option A
Quick Check:
Use isset and strict null check to allow 0 [OK]
Hint: Use isset and !== null to allow zero values [OK]
Common Mistakes:
Using empty excludes zero values
Using OR instead of AND
Confusing is_null with isset
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.