3

Is it faster to declare array before using the array php?

For example, is it faster to declare an array and then set a value:

$arr = [];
$arr['foo'] = 'bar';

Or just jump right into setting a value without declaring the object first:

$arr['foo'] = 'bar';

Please note, I do not want to do the following:

$arr = ['foo' => 'bar'];

3 Answers 3

5

I always declare because I don't want to run into undefined vars errors. But I was curious as well. Let's test!

$size = 1000000;
$start_time = microtime(true);
for($i=1;$i<=$size;$i++):
    $arr = [];
    $arr['foo']='bar';
    unset($arr);
endfor;
$results['declare first'] =
    floor((microtime(true)-$start_time) * 1000).' milliseconds';

unset($arr, $i, $start_time);

$start_time = microtime(true);
for($i=1;$i<=$size;$i++):
    $arr['foo']='bar';
    unset($arr);
endfor;
$results['do not declare'] =
    floor((microtime(true)-$start_time) * 1000).' milliseconds';

print_r($results);

Typical result on my PC: PHP 5.6.23 on Win 7 x64:

[
    'declare first' =>  '464 milliseconds',
    'do not declare' => '376 milliseconds',
]

A 100-millisecond difference after 1 million ops means you basically won't notice the difference unless you have several million arrays. So declare first :-)

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

5 Comments

This is not an apples to apple comparison since you're declaring the array at every iteration in the first block but only (implicitly) declaring it once in the second block. A valid test would involve putting "unset ( $arr['foo'] );" under "$arr['foo']='bar';" in both blocks.
I did not even know that you could write loops with for(): and endfor, I have always just done for {}
@BenShoval You're right! thanks. I edited the test, and the previous results are confirmed. However, the difference in percentage terms isn't as pronounced
@ZachPanzarino I love using for...endfor, if...endif, foreach...endforeach and while...endwhile. Makes code easier to read for me than have just a closing bracket
I feel exactly the opposity, brackets are much easier for me to recognize code blocks by
4

While there is a speed benefit to not declaring (see BeetleJuice's answer), there is a big potential downside:

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_FATAL because the array doesn't exist.

quoted from: Is it necessary to declare PHP array before adding values with []?

2 Comments

That answer doesn't claim that there is no speed benefit. In fact, testing shows the opposite to be true (see my answer). Still, I advocate declaring.
@BeetleJuice - Thank you for revising your answer based upon my comment. You are obviously correct. I've changed my answer to reflect this.
3

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_FATAL because 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.

1 Comment

Good info, but it doesn't answer the OP's question, which was about speed: "Is it faster to declare array before using the array php?"

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.