9

For example:

$array = [];

echo $array['bar']; // PHP NOTICE - trying to access not existing key`

$array['bar'][] = 'foo'; // Nothing

I understand that it creates array with that index 'bar', but how does PHP deals with that internally?

6
  • That's just how it works. Commented Dec 12, 2016 at 16:40
  • 3
    @AbraCadaver That's how a religion starts, don't question just follow. OPs question is legitimate. Your answer is not. Commented Dec 12, 2016 at 16:42
  • 1
    @Andrew: That's funny, I didn't post an answer. Commented Dec 12, 2016 at 16:44
  • Comment, whatever. Point stands. Commented Dec 12, 2016 at 16:45
  • 2
    This article will explain more about how PHP is internally handling arrays: nikic.github.io/2012/03/28/… Commented Dec 12, 2016 at 17:04

1 Answer 1

4

$array['bar'][] = 'foo'; doesn't return a notice or error because there is no error. You are creating a new array index, and another index within that, and assigning a value to it. That's what the statement means. There's no error to return.

If you want to have behaviour for if a particular array index is not set, you can use array_key_exists (http://php.net/manual/en/function.array-key-exists.php):

if(array_key_exists('bar', $array)){
    $array['bar'][] = 'foo';
} else {
    // something else
}

That's if this question is functional (ie., you're trying to accomplish something specific). If the question is more conceptual - why doesn't PHP read the variable assignment as an error:

PHP is capable of initializing and assigning a variable in one line, ie $foo = 'bar'. This does not return an error even though $foo was not previously defined because PHP initializes the variable first. The same method holds true for array indexes. $array['foo'][] = 'bar' doesn't return an error or a notice because PHP is initializing the array index just as it would a variable.

Sign up to request clarification or add additional context in comments.

6 Comments

You still didn't answered the question, namely why (and how does PHP internally handle that) it DOES throw a notice when you're trying to access an inexisting key, but it does not throw any notice when you're trying to set a value for an inexisting key of an array
Because functionally speaking, you AREN'T trying to set a value for a nonexistent key. You're setting a key, and another key inside it, and assigning a value to THAT key. PHP is intelligent enough to cascade that key creation down. That's opposed to when you try to ACCESS a key that doesn't exist, in which case there's nothing PHP can do.
@Dragos The proceducre of creating a previously non-existing variable is called initialization. In PHP you can also initialize a variable by assigning a value to previously non-existing variable. There wouldn't be any notice in this case either. It is the same when you set a value for new key in an array.
@PeregrineStudios thanks for answer and yes, my question is more conceptual. I can understand how PHP deals with that in abstract sense, but the part 'PHP is able to correct' is a bit mistery for me:)
Clarified my answer a little including Dragos' comment. I hope it clears things up a bit.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.