Re: Array dereferencing of scalars

From: Date: Tue, 09 Jun 2015 12:38:44 +0000
Subject: Re: Array dereferencing of scalars
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-86519@lists.php.net to get a copy of this message
Christoph Becker wrote on 08/06/2015 22:11:
The behavior of dereferencing scalars as if they were arrays or strings is arguable: <?php $foo = 42; $foo['bar']; // => NULL
A few things to note here: - accessing a *numeric* offset of a *string* is valid (selects a single character) - accessing a *string* offset of another string is interpreted as access to character 0, with a Warning issued - de-referencing this to two levels ($string['key']['key']) and then attempting to write is a Fatal error - as discussed elsewhere, coercing a NULL to an array seems perfectly reasonable - assigning to an offset of anything other than a string or array results in a Warning and no change to the variable While I agree that it's kind of surprising not to get even a Notice in your example, I'm not 100% sure what the behaviour should be, given how many different scenarios already have different behaviour. Maybe someone needs to come up with a generalised rule? Regards, -- Rowan Collins [IMSoP]

« previous php.internals (#86519) next »