1

前几天我遇到了这个 php 语法,我不熟悉它。我*猜它可能会推动,但我真的不知道。这 * 和 . 完全一样吗array_push($b)?如果它正在完成*类似的事情,请解释它有何不同。

    $foo = array();
    foreach($bar as $b)
    {
        $foo[] = $b; //push?
    }
4

2 回答 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.

于 2012-05-02T20:52:11.980 回答
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.

于 2012-05-02T20:57:07.227 回答