PHP

Check whether an array key is undefined:

if (isset($array['age']))
+ Fast and does not trigger warnings.
- Returns false for keys that exist but contain null.

if (array_key_exists('age', $array))
+ Pros: Detects even null values.
- Slightly slower than isset().

$age = $array['age'] ?? 'Unknown'; // If 'age' is undefined, use 'Unknown'
+ The cleanest way to provide default values.
- Requires PHP 7+.

Rate this FAQ

0 (0 Votes)