$arr = array(); // is this line needed?
$arr[] = 5;
I know it works without the first line, but it’s often included in practice.
What is the reason? Is it unsafe without it?
I know you can also do this:
$arr = array(5);
but I’m talking about cases where you need to add items one by one.
If you don’t declare a new array, and the data that creates / updates the array fails for any reason, then any future code that tries to use the array will
E_FATALbecause the array doesn’t exist.For example,
foreach()will throw an error if the array was not declared and no values were added to it. However, no errors will occur if the array is simply empty, as would be the case had you declared it.