0

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?
3
  • 3
    c'mon man: sandbox.onlinephpfunctions.com/code/… Commented Feb 3, 2015 at 20:54
  • Why the downvote? I have written the question because I am only on my mobile for some days and decided to learn PHP by reading through the specs. That's why this practical question is asked so "theoretically". Commented Feb 3, 2015 at 20:59
  • 2
    that's no excuse for not testing it first yourself. We're happy to help, but not when you can't be bothered to even try things yourself. Commented Feb 3, 2015 at 21:00

3 Answers 3

4

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

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

5 Comments

Will $d[0] also not work? Or more generally spoken will it only work as left hand side of an assignment?
You can easily test that @MinecraftShamrock
No. If you attempt to do anything with an array index or key that hasn't already been defined, other than define it or check isset(), empty(), or array_key_exists() on it, you'll get a warning.
Actually it will be undefined variable d because $d doesn't exist.
Abra's right. I wasn't paying enough attention to the variable names.
1

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.

3 Comments

The [] intrinsics can also be used for string manipulation. But if the variable is not defined yet, will my examples always create a new array? Or is it possible to create a string with given character at given index using this?
If the variable is not defined, this method will create an array not a string. You cannot create a 'part of string'.
$var[] syntax will always create an array if $var is undefined. If $var is defined the results will vary based on the content.
1

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.

Comments

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.