Possible Duplicate:
array_push() vs. $array[] = … Which is fastest?

Is it better to use [] or array_push() to add to an array in PHP? I always use array_push(), but only because it /seems/ proper.

$array = array();

array_push($array, array('1','2','3'));

// or

$array[] = array('1','2','3');

Just curious.

share|improve this question
its better to use array_push() to add elements in an php array – Mohan Ram Dec 22 '10 at 14:50
See the second answer of the duplicate question. You question is not an exact duplicate but that answer is good for this one too. – Alin Purcaru Dec 22 '10 at 14:51

marked as duplicate by Haim Evgi, Alin Purcaru, Jon, mario, Josh Dec 22 '10 at 14:52

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3 Answers

up vote 1 down vote accepted

I believe $array[] is more efficient.

share|improve this answer
Turns out thats because i've seen it here : stackoverflow.com/questions/2431629/php-array-push-vs-myarray - do I delete my answer as it's a dup? – piddl0r Dec 22 '10 at 14:50

In terms of readability i prefer array_push() compare to []. However, array_push is abit slow in performance compare to array_push vs []

share|improve this answer

When comparing alternative syntax constructs, there are multiple attributes to take into consideration:

  • "better"
  • faster
  • readability

Only one of them is valid.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.