Accessing a non-existent key returns a null, and then the null is assigned
to $a.

If you use an intermediate value, it illustrates that there's no way for
the assignment statement to "know" that the R-value (value on the right
side of the equal sign) evaluated to null -- and probably threw a NOTICE --
before being assigned. Null "erases" the original value as opposed to
passing over it invisibly.

$a = $numbers['a'];
$d = $numbers['d']; // $d is now null
$a = $d; // $a is now null

By the way, if you're using PHP 7 you can use the null coalesce operator,
whereby this:

$a = isset($numbers['d']) ? $numbers['d'] : $a;

Becomes this:

$a = $numbers['d'] ?? $a;

Richard

On Tue, Jul 5, 2016 at 10:27 PM Wade Shearer <wadeshearer.li...@me.com>
wrote:

> Consider this:
>
>
> $numbers = array('a' => 1, 'b' => 2, 'c' => 3);
>
> $a = $numbers['a'];
> $a = $numbers['d'];
>
>
> I would expect $a to equal 1. Since the key ‘d’ doesn’t exist, I would
> expect the value of $a to not change. It is set to blank though. Any
> explanation why this happens?
>
> Thanks.
>
> _______________________________________________
>
> UPHPU mailing list
> UPHPU@uphpu.org
> http://uphpu.org/mailman/listinfo/uphpu
> IRC: #uphpu on irc.freenode.net

_______________________________________________

UPHPU mailing list
UPHPU@uphpu.org
http://uphpu.org/mailman/listinfo/uphpu
IRC: #uphpu on irc.freenode.net

Reply via email to