Create non-existing array by accessing it in PHP?


I know that it is possible to add/modify array elements like this:

$a = ['zero', 'one', 'two']; //first create array
$a[4] = 'four';
$a[] = 'three';

Here I created the array before modifying it using[]. But what happens if I do not create the variable with the array first? Will a new array be created and assigned to the variable automatically?

$b[2] = 'two'; // will create array?
$c[] = 'zero'; // will create array?
echo $d[1]; // will create array?



$b[2] = 'two'; will create an array.

$c[] = 'zero'; will create an array.

echo $d[1]; will throw an undefined variable $d warning, or undefined offset 1 warning if $d exists but $d[1] does not.




Don't do that.

It's true that$array[1] = 'foo'; will create an array but not always. If you already have a variable$array then you won't get new array.




The short answer to your question is: Yes, if you access it for writing, PHP will create an array but not always!

To be more specific:

  • {-code-1} - will create an array and will store the string{-code-2} at key2;
  • $c[] = 'zero'; - will create an array and will store the stringzero at key0;
  • echo $d[1] - will NOT create an array; and it will NOT create$d[1] if$d already is an array but it doesn't have the key1.

You can check all these for yourself using or .

The complete answer is: DON'T do that! Always initialize the variables before using them!

Let me show you what happens when you ignore this advice and rely on PHP doing the initialization for you:

// Skip over the initialization of $a, PHP will do it for you
$a[1] = 'a';
$a = array_merge($a, array('b'));

This code will produce the expected result:

    [0] => a
    [1] => b

Now, let's say several hundreds lines of code above in the same function the code looks like this:

// Somewhere on top of the function (or file, if not in a function)
$a = 'blah';
$b = $a.', blah';
// there is no need for $a below this line but we don't bother
// to unset it, PHP will do it anyway at the end of the function
// ...
// a lot of code here that doesn't use $a
// ...
// several hundreds of lines below
// ...
// you forgot completely about the code on top
// ...
// and write new code as below:

// Skip over the initialization of $a, PHP will do it for you
$a[1] = 'a';

Oops,array_merge() failed with the messagePHP Warning: array_merge(): Argument #1 is not an array.

What happened?

You expects that PHP will construct an array for you because you have used$a like$a[1] = 'b';

But PHP doesn't work this way!
It cannot guess what are you thinking. Variable$a already existed (it was created on top of the function and nobody unset it) and was holding a string. Because the notation$a[1] = 'b'; is a valid way to change a single character of a string, PHP just did that, completely unaware that you wish it to create an array.


Always, but always initialize your variables just before you use them. Never expect that PHP or some fairies will do that for you. Maybe they do it now but they may change their mind tomorrow and suddenly your code will stop working or it will start producing bad results.

People are also looking for solutions to the problem: php - Saving image on db mysql through swift


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.