Wikis & learning

Tell an absent array key from a null value

Inspect the exact array in the same request and scope as the failing access. A browser form might omit an unchecked field entirely. A key may also exist with a null value.

Question

Tell an absent array key from a null value

Editorial answer

Inspect the exact array in the same request and scope as the failing access. A browser form might omit an unchecked field entirely. A key may also exist with a null value.

Use array_key_exists when null is meaningful. Use isset to ask whether the key exists with a non-null value. Read with the null-coalescing operator for an optional display field. Validate required fields before accessing them.

Verify the result

Check four inputs separately: an empty array, a key set to null, an empty string and the string 0. Avoid treating all four as missing.

Discussion

Sign in to participate.