前几天我遇到了这个 php 语法,我不熟悉它。我*猜它可能会推动,但我真的不知道。这 * 和 . 完全一样吗array_push($b)
?如果它正在完成*类似的事情,请解释它有何不同。
$foo = array();
foreach($bar as $b)
{
$foo[] = $b; //push?
}
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.
$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.