Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>No benchmarks, but I personally feel like <code>$array[]</code> is cleaner to look at, and honestly splitting hairs over milliseconds is pretty irrelevant unless you plan on appending hundreds of thousands of strings to your array.</p> <p><strong>Edit</strong>: Ran this code:</p> <pre><code>$t = microtime(true); $array = array(); for($i = 0; $i &lt; 10000; $i++) { $array[] = $i; } print microtime(true) - $t; print '&lt;br&gt;'; $t = microtime(true); $array = array(); for($i = 0; $i &lt; 10000; $i++) { array_push($array, $i); } print microtime(true) - $t; </code></pre> <p>The first method using <code>$array[]</code> is almost 50% faster than the second one.</p> <h3>Some benchmark results:</h3> <pre><code>Run 1 0.0054171085357666 // array_push 0.0028800964355469 // array[] Run 2 0.0054559707641602 // array_push 0.002892017364502 // array[] Run 3 0.0055501461029053 // array_push 0.0028610229492188 // array[] </code></pre> <p>This shouldn't be surprising, as the PHP manual notes this:</p> <blockquote> <p>If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function. </p> </blockquote> <p>The way it is phrased I wouldn't be surprised if <code>array_push</code> is more efficient when adding multiple values. <strong>EDIT</strong>: Out of curiosity, did some further testing, and even for a large amount of additions, individual <code>$array[]</code> calls are faster than one big <code>array_push</code>. Interesting.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload