PHP empty() Equivalent in Python

PHP's empty() is essentially a null-safe truthiness check; Python's `not value` covers the same intent, but the specific set of values each language considers falsy is not identical.

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

PHP source
empty()
Closest Python API
not (truthiness check)

Main limitation: PHP's `empty($var)` treats `""`, `"0"` (the *string* zero), `0`, `0.0`, `null`, `false`, and `[]` as empty. Python's falsy set includes `""`, `0`, `0.0`, `None`, `False`, and empty containers (`[]`, `{}`, `()`, `set()`) — but critically, Python's `"0"` string is truthy, while PHP's is falsy. This is the single most common porting bug for this function.

How to use each function

Before comparing the two, here's how empty() and not (truthiness check) work on their own.

PHP: empty()

empty(mixed $var): bool

A language construct that safely tests truthiness without ever warning on an undefined variable or missing array key. It returns true for a fixed set of "empty" values: '', '0' (the string zero), 0, 0.0, null, false, and []. Effectively shorthand for !isset($var) || !$var.

Returns: true if the variable is unset or holds one of PHP's empty values, false otherwise.

PHP
empty([]);   // true
empty("0");  // true — PHP-specific quirk
empty("hi"); // false

Python: not (truthiness check)

not value

Negates Python's own truthiness test. Falsy values are '', 0, 0.0, None, False, and empty containers ([], {}, (), set()) — notably the string "0" is truthy in Python, unlike PHP's empty(), which is the single most common porting gotcha for this pair.

Returns: True if value is falsy, False otherwise.

Python
not []      # True
not "0"     # False — differs from PHP's empty("0")

Quick mapping

PHP empty()
Closest Python API not (truthiness check)

How it differs

  • PHP's empty($var) treats "", "0" (the *string* zero), 0, 0.0, null, false, and [] as empty. Python's falsy set includes "", 0, 0.0, None, False, and empty containers ([], {}, (), set()) — but critically, Python's "0" string is truthy, while PHP's is falsy. This is the single most common porting bug for this function.
  • PHP's empty() never raises a warning or error for an undefined variable or array key — it safely treats undefined as empty, unlike a bare truthiness check on an undefined variable (which would error). Python's not variable requires the variable to already exist — use not data.get("key") for the safe, isset()-like nested-access behavior.
  • Both languages treat empty containers (arrays/lists, objects/dicts) as falsy/empty, so that part of the behavior translates directly.

Examples

Basic empty check

PHP
empty([]); // true
empty(0); // true
empty(null); // true
Python
not []  # True
not 0  # True
not None  # True
These common cases match exactly between the two languages.

The "0" string divergence

PHP
empty("0"); // true — PHP-specific quirk
Python
not "0"  # False — Python considers "0" truthy
This is the key gotcha: PHP special-cases the string "0" as empty for historical reasons (going back to Perl-influenced boolean conventions); Python does not.

Edge cases

Edge case

If your PHP logic checks empty($str) and $str might be the literal string "0" (e.g. a form field value), porting straight to not s in Python will behave differently: PHP considers "0" empty, Python considers "0" truthy. Add an explicit check like not s or s == "0" only if you need to replicate PHP's behavior exactly — otherwise this is usually a sign the PHP behavior itself was surprising.

References

  • PHP Manual — empty — checked 2026-07-29
  • Python docs — Truth Value Testing (v3.x) — checked 2026-07-29

Last verified 2026-07-29.