1

I ran into this php syntax the other day and I am not familiar with it. I *guessed it might doing a push, but I really don't know. Is this *exactly the same as array_push($b). If it is accomplishing something *similar, please explain how it is different.

    $foo = array();
    foreach($bar as $b)
    {
        $foo[] = $b; //push?
    }
1
  • The only difference is that array_push can push more than 1 element: array_push($array, $var1, $var2) is the same as $array[] = $var1; $array[] = $var2; Commented May 2, 2012 at 20:55

2 Answers 2

4

The only difference is the tiny bit of extra overhead involved in making a function call to array_push() vs making use of the language construct [] to append onto an array. They are functionally equivalent.

The difference between them from that function call is going to be utterly miniscule to the degree that you needn't worry about it unless you are doing it millions of times.

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

Comments

2

$foo[] = $b will be slightly faster due to the overhead of a function call (as Michael stated below).

Additionally, as stated in the manual, if the first argument to array_push is not an array a notice is raised. Using the array bracket notation will simply create a new array if it does not already exist.

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.