isset() vs empty() in PHP with Truth Table
isset() asks 'does it exist and is not null?'; empty() asks 'is it falsy?' — 0, "", "0", null, false all count as empty. Full truth table and form-validation traps.
Two different questions
$marks = 0; — it exists (isset true) but is empty (empty true).The truth table (screenshot-worthy)
| Value of $x | isset($x) | empty($x) |
|---|---|---|
| $x = "Aman" | true | false |
| $x = 0 | true | true ⚠ |
| $x = "0" | true | true ⚠ |
| $x = "" | true | true |
| $x = null | false | true |
| $x = false | true | true |
| $x = array() | true | true |
| $x never declared | false (no error!) | true (no error!) |
Two rows deserve a star: 0 and "0" are empty — the source of most real-world bugs with these functions. And note the last row: both functions are safe on undeclared variables — no warning — which is exactly why we use them instead of if ($x).
The classic bug: marks entry form
<?php
// Teacher enters marks. Student scored ZERO (absent ke marks nahi, sach me 0).
$marks = $_POST['marks']; // "0"
if (empty($_POST['marks'])) {
echo "Error: please enter marks!"; // WRONG! Fires for 0!
}
// The student DID get marks - zero is a valid entry.
// empty() treats "0" as nothing and rejects an honest zero.
// CORRECT check: field missing or truly blank, but 0 allowed:
if (!isset($_POST['marks']) || $_POST['marks'] === '') {
echo "Error: please enter marks!";
} else {
$marks = (int) $_POST['marks']; // 0 accepted happily
}
?>
isset() + === '' instead. This one bug has rejected countless honest zeros in real school and billing software.Where each one shines
// isset(): did the form field / URL parameter arrive at all?
if (isset($_GET['page'])) { ... }
if (isset($_SESSION['user_id'])) { ... } // is user logged in?
// empty(): is there genuinely nothing useful here?
if (empty($cart)) { echo "Your cart is empty"; } // [] or unset - both covered
if (empty($search)) { echo "Type something to search"; }
// PHP 7 bonus - null coalescing, the clean modern pattern:
$page = $_GET['page'] ?? 1; // means: isset($_GET['page']) ? $_GET['page'] : 1
The ?? operator is literally isset() in disguise — it works on the same rule (exists and not null). On PHP 7.2 hosting it is fully available and makes code beautifully short.
Summary
- isset() = existence check. True unless the variable is undeclared or null.
- empty() = usefulness check. True for all falsy values — including the dangerous 0 and "0".
- Relation:
empty($x)behaves like!isset($x) || $x == false. - Interview line: "isset asks 'is it there?', empty asks 'is it worthless?' — and the trap is that 0 and '0' count as worthless, so never validate numeric fields with empty()."
दो अलग सवाल
$marks = 0; — exist करता है (isset true) पर empty है (empty true).Truth table (screenshot लेने लायक)
| $x की value | isset($x) | empty($x) |
|---|---|---|
| $x = "Aman" | true | false |
| $x = 0 | true | true ⚠ |
| $x = "0" | true | true ⚠ |
| $x = "" | true | true |
| $x = null | false | true |
| $x = false | true | true |
| $x = array() | true | true |
| $x कभी declare नहीं हुआ | false (कोई error नहीं!) | true (कोई error नहीं!) |
दो rows पर star बनता है: 0 और "0" empty हैं — इन functions के ज़्यादातर real-world bugs की जड़. और आखिरी row देखिए: दोनों functions undeclared variables पर safe हैं — कोई warning नहीं — इसीलिए हम if ($x) की जगह इन्हें use करते हैं.
Classic bug: marks entry form
<?php
// Teacher marks भरता है. Student का सच में ZERO आया (absent नहीं, सचमुच 0).
$marks = $_POST['marks']; // "0"
if (empty($_POST['marks'])) {
echo "Error: please enter marks!"; // GALAT! 0 पर भी fire!
}
// Student को marks मिले हैं - zero valid entry है.
// empty() "0" को कुछ-नहीं मानकर ईमानदार zero reject कर देता है.
// SAHI check: field गायब या सच में blank, पर 0 allowed:
if (!isset($_POST['marks']) || $_POST['marks'] === '') {
echo "Error: please enter marks!";
} else {
$marks = (int) $_POST['marks']; // 0 खुशी-खुशी accepted
}
?>
isset() + === '' use करें. इस एक bug ने असली school और billing software में अनगिनत ईमानदार zeros reject किए हैं.कौन कहां चमकता है
// isset(): form field / URL parameter आया भी या नहीं?
if (isset($_GET['page'])) { ... }
if (isset($_SESSION['user_id'])) { ... } // user logged in है?
// empty(): यहां सच में कुछ काम का नहीं है?
if (empty($cart)) { echo "Your cart is empty"; } // [] या unset - दोनों covered
if (empty($search)) { echo "Type something to search"; }
// PHP 7 bonus - null coalescing, साफ modern pattern:
$page = $_GET['page'] ?? 1; // मतलब: isset($_GET['page']) ? $_GET['page'] : 1
?? operator असल में भेस बदला हुआ isset() है — same rule पर चलता है (exists और null नहीं). PHP 7.2 hosting पर पूरी तरह available है और code खूबसूरती से छोटा कर देता है.
निचोड़
- isset() = existence check. Variable undeclared या null न हो तो true.
- empty() = usefulness check. हर falsy value पर true — खतरनाक 0 और "0" समेत.
- रिश्ता:
empty($x)का behaviour है!isset($x) || $x == false. - Interview line: "isset पूछता है 'है क्या?', empty पूछता है 'बेकार है क्या?' — trap यह कि 0 और '0' बेकार गिने जाते हैं, इसलिए numeric fields कभी empty() से validate न करें."
Frequently Asked Questions
What is the difference between isset() and empty()?
isset() returns true when a variable exists and is not null; empty() returns true for all falsy values — 0, "0", "", null, false, empty array — so a variable can be set yet empty, like $marks = 0.
Why should empty() not be used to validate numeric form fields?
Because empty("0") and empty(0) are true — a legitimate zero entry for marks, quantity or discount gets rejected; use isset() with a strict === "" check instead.
What does the ?? null coalescing operator do?
$x = $a ?? $b assigns $a if it is set and not null, otherwise $b — a compact replacement for the isset() ternary pattern, available from PHP 7.