PHP isset() Equivalent in Python

PHP's isset() checks both "does this key/variable exist" and "is it not null" in one call — Python has no single function that does both, so the right equivalent depends on which behavior you actually need.

Requires review Works only under specific conditions — read the caveats before relying on it.

PHP source
isset()
Closest Python API
in operator / dict.get()

Main limitation: PHP's `isset($var)` returns `false` for both an undefined variable *and* a variable explicitly set to `null` — it cannot distinguish "never set" from "set to null". Python's `"key" in dict` only checks existence and correctly returns `True` even if the value is `None`; use `dict.get("key") is not None` if you specifically need PHP's combined behavior.

How to use each function

Before comparing the two, here's how isset() and in operator / dict.get() work on their own.

PHP: isset()

isset(mixed $var, mixed ...$vars): bool

A language construct (not a real function) that checks whether one or more variables are set and not null in a single call, without triggering an undefined-variable warning. Passing multiple arguments returns true only if every one of them is set. It cannot distinguish a variable that was never defined from one explicitly set to null.

Returns: true only if every given variable/key exists and is not null.

PHP
$data = ["name" => "Ada"];
isset($data["name"]); // true
isset($data["age"]);  // false (key missing)

Python: in operator / dict.get()

key in dict / dict.get(key, default=None)

Python splits isset()'s combined "exists and is not null" check into two independent tools: in tests key existence only (True even if the value is None), while dict.get(key) returns the value or None if missing. Combine them — dict.get("key") is not None — to fully replicate isset().

Returns: in returns a bool; get() returns the stored value or its default.

Python
data = {"age": None}
"age" in data                 # True — key exists
data.get("age") is not None   # False — matches isset() here

Quick mapping

PHP isset()
Closest Python API in operator / dict.get()

How it differs

  • PHP's isset($var) returns false for both an undefined variable *and* a variable explicitly set to null — it cannot distinguish "never set" from "set to null". Python's "key" in dict only checks existence and correctly returns True even if the value is None; use dict.get("key") is not None if you specifically need PHP's combined behavior.
  • For array/dict keys specifically, PHP's isset($arr["key"]) maps most directly to Python's "key" in arr and arr["key"] is not Nonein alone is not a full equivalent because of the null-handling difference above.
  • Accessing an undefined variable in PHP without isset() raises a warning (or in strict contexts, can error) but doesn't crash; accessing an undefined variable in Python raises a NameError immediately, and there's no direct Python equivalent for checking variable existence before use in the same way — restructure the code to always initialize variables instead.

Examples

Checking if a dict/array key exists and is non-null

PHP
$data = ["name" => "Ada"];
isset($data["age"]); // false (key missing)
Python
data = {"name": "Ada"}
data.get("age") is not None  # False (key missing)
Both correctly report false when the key is entirely absent.

The null-vs-missing distinction PHP conflates

PHP
$data = ["age" => null];
isset($data["age"]); // false — same as if missing!
Python
"age" in {"age": None}  # True — Python correctly distinguishes "present but None"
This is the core behavioral gap: PHP's isset() treats a null value exactly like a missing key, while Python's `in` operator (correctly, for most purposes) treats them as different states.

Edge cases

Edge case

For dict access specifically, dict.get("key") returns None for a missing key, matching neither PHP behavior exactly — combine .get() with an explicit is not None check to fully replicate isset($arr["key"]).

Edge case

isset() on a nested, possibly-missing path (isset($data["a"]["b"])) short-circuits safely in PHP if "a" itself doesn't exist; the equivalent nested .get() chain in Python needs a default at each level, e.g. data.get("a", {}).get("b"), to avoid a KeyError/AttributeError.

References

  • PHP Manual — isset — checked 2026-07-29
  • Python docs — dict.get() (v3.x) — checked 2026-07-29

Last verified 2026-07-29.