MinecraftShamrock
MinecraftShamrock

Reputation: 3574

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?

Upvotes: 0

Views: 332

Answers (3)

axiac
axiac

Reputation: 72177

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:

  • $b[2] = 'two'; - will create an array and will store the string two at key 2;
  • $c[] = 'zero'; - will create an array and will store the string zero at key 0;
  • 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 key 1.

You can check all these for yourself using print_r() or var_dump().

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'));
print_r($a);

This code will produce the expected result:

Array
(
    [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';
array_merge($a);

Oops, array_merge() failed with the message PHP 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.

Conclusion?

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.

Upvotes: 1

T.J. Compton
T.J. Compton

Reputation: 405

$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.

Upvotes: 4

Brejk
Brejk

Reputation: 454

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.

Upvotes: 1

Related Questions