This question was not yet really answered (albeit they found a way to foreach over the array to look-up the 'guest'
key in the $_SESSION
array).
The problem is to check an array entry that is null. Reason is that a check with isset($_SESSION['guest'])
returns false as the member is null
. Cf. https://php.net/isset .
A function that works is array_key_exists(), as it takes all members into account, including those which are null
. It really tests if the key exists in the array.
Example for the case that the guest member is null:
$_SESSION['guest']; # null, no warning
isset($_SESSION['guest']); # false
array_key_exists('guest', $_SESSION); # true
Naturally, using foreach works as well, but it requires more code and the functionality already exists with the array_key_exists() function.
Reference: https://php.net/array_key_exists – see especially Example #2 array_key_exists() vs isset().