isset($var) returns true if the variable exists and its value is not null. It returns false for an undefined variable, an unset one, or one holding null, and it never prints a warning.
isset() only looks at whether something is there. A value that is set but empty, like 0, "" or [], still returns true. If you want "set and not empty", use empty (negated).
Check if an array key exists with isset
This is where isset() is used most. Reading a key that does not exist prints Warning: Undefined array key on a normal PHP setup; checking it with isset() first does not. It also works for nested keys, and stops quietly at the first missing level.
When you only need the value or a default, $user["website"] ?? "none" does the same check in one step. See the null coalescing operator.
isset with multiple variables
Pass several arguments and isset() returns true only if all of them are set and not null. It is a short way to check that every required field arrived.
isset on strings and objects
On a string, isset($str[5]) checks whether there is a character at offset 5, which is a quick length check. On an object, it checks a property; for a class with private data, isset() calls the __isset() method if the class defines one.
isset vs empty vs is_null
Pick a value of $x in the table to compare the three checks and the plain (bool) cast. Every cell was computed by PHP 8.3.
| $x | isset | empty | is_null | bool |
|---|---|---|---|---|
| null | ||||
| false | ||||
| 0 | ||||
| "0" | ||||
| "" | ||||
| " " | ||||
| [] | ||||
| "a" | ||||
| 1 | ||||
| 0.0 | ||||
| "0.0" |
The pattern to remember:
isset()isfalseonly fornulland things that do not exist.empty()istruefor those and also for every falsy value:false,0,0.0,"","0",[].is_null()gives the opposite ofisset()for existing variables, but warns when the variable is undefined. Useisset()or=== nullinstead.
Check if a form was submitted
isset() is the classic way to see whether a form field arrived. Run this block, type a name and press Send: the same script runs again with $_POST filled in.
An empty text field is still sent, as "", so isset($_POST["name"]) is true after any submit. An unchecked checkbox is not sent at all, which makes isset() exactly the right test for checkboxes. To require a non-empty value, check the trimmed string as the block does. The forms page builds a full form with validation.
isset vs array_key_exists
The two agree on everything except a key whose value is null. isset() treats it as missing, array_key_exists() does not.
If "set to null" means something different from "not configured" in your data, use array_key_exists(). Otherwise isset() is shorter, and it is a language construct rather than a function call, so it is also slightly faster.
Common mistake: isset on an expression
isset() only accepts variables, array elements and properties. Passing a function result or a constant is a compile error:
<?php
if (isset(trim($input))) {}
PHP Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead) in /home/index.php on line 2
Store the result in a variable first, or test the value itself: null !== $expr as the message suggests, or trim($input) !== "" for a string.
Frequently Asked Questions
What does isset() do in PHP?
isset($x) returns true if $x exists and is not null, and false otherwise. It never raises a warning for an undefined variable or array key, so it is safe on request data like isset($_POST['email']).
What is the difference between isset() and empty() in PHP?
isset() asks "does this exist and is it not null"; empty() asks "is this missing or falsy". For 0, "", "0" and [], isset() returns true and empty() also returns true. For null or a missing key, isset() is false and empty() is true. !empty($x) is the same as isset($x) && $x being truthy.
Can isset() check multiple variables?
Yes: isset($a, $b, $c) returns true only if every one of them is set and not null. It stops at the first one that is not set.
Should I use isset() or array_key_exists()?
They differ only when the key exists with the value null: isset($arr['k']) returns false, array_key_exists('k', $arr) returns true. Use array_key_exists when null is a meaningful stored value; otherwise isset is shorter and slightly faster.