PHP in_array() Equivalent in Python
Python's `in` operator replaces PHP's in_array() for membership tests, but PHP's default loose comparison can produce true where Python's `in` (which always compares by equality and type) would say false.
Requires review Works only under specific conditions — read the caveats before relying on it.
- PHP source
in_array()- Closest Python API
in
Main limitation: PHP `in_array($needle, $haystack)` defaults to loose (`==`) comparison, so `in_array("abc", [0, 1, 2])` is `true` in older PHP behavior due to string-to-number coercion quirks; Python's `in` never coerces types this way — `"abc" in [0, 1, 2]` is always `False`.
How to use each function
Before comparing the two, here's how in_array() and in work
on their own.
PHP: in_array()
in_array(mixed $needle, array $haystack, bool $strict = false): bool
Checks whether a value exists anywhere in an array. By default it uses loose (==) comparison, which can produce surprising type-coercion matches; passing true as the third argument forces strict (===) comparison.
Returns: true if the value is found, false otherwise.
$roles = ["admin", "editor", "viewer"];
in_array("editor", $roles); // true
in_array("0", [0, 1, 2], true); // false (strict) Python: in
value in iterable
A built-in operator, not a function, that tests membership by equality across lists, tuples, sets, strings, and dict keys. Unlike PHP's default loose in_array(), Python's in never coerces types — it only matches values of an equal type and value.
Returns: True if a matching element exists, False otherwise.
"editor" in ["admin", "editor", "viewer"] # True Quick mapping
| PHP | in_array() |
|---|---|
| Closest Python API | in |
How it differs
- PHP
in_array($needle, $haystack)defaults to loose (==) comparison, soin_array("abc", [0, 1, 2])istruein older PHP behavior due to string-to-number coercion quirks; Python'sinnever coerces types this way —"abc" in [0, 1, 2]is alwaysFalse. - PHP's third
$strictargument (in_array($needle, $haystack, true)) forces type-and-value comparison, which then matches Python'sinsemantics much more closely — prefer strict mode when translating to avoid surprises. - Python's
inworks on any iterable (lists, tuples, sets, strings, dict keys) with the same syntax; PHP'sin_array()is specifically for arrays, withstr_contains()/strpos()used for strings instead.
Examples
Basic membership check
in_array("b", ["a", "b", "c"]); // true "b" in ["a", "b", "c"] # True Strict comparison to avoid type coercion
in_array("1", [0, 1, 2], true); // false (strict) "1" in [0, 1, 2] # False Edge cases
Edge case
Always use PHP's strict mode (in_array($needle, $haystack, true)) when porting to Python's in, since Python has no loose-comparison equivalent to translate exactly — otherwise the behavior can silently diverge for mixed-type arrays.
Edge case
For frequent membership checks against a large collection, Python's in on a set is O(1) average case, versus O(n) for a list — consider converting to a set if performance matters, mirroring how PHP developers sometimes flip an array to use isset() on keys instead.
References
- PHP Manual — in_array — checked 2026-07-29
- Python docs — Comparisons — the `in` operator (v3.x) — checked 2026-07-29
Last verified 2026-07-29.