3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function bytesToSize($bytes, $precision = 2) { $symbols = array('B', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb', 'Eb', 'Zb', 'Yb'); $exp = (int)floor(log($bytes) / log(1024)); return sprintf('%.'. $precision .'f '. $symbols[$exp], $bytes / pow(1024, floor($exp))); } function mem() { return bytesToSize(memory_get_usage(true)); } function test($msg, $test) { $t = microtime(true); $test($msg); echo $msg .': <b>'. sprintf('%.6f', microtime(true) - $t) .'</b><br>'; } $array = range(0, 300000); test('foreach', function ($msg) use ($array) { echo $msg . ' before loop mem: '. mem() .'<br>'; foreach ($array as $v) { $b = $v; } echo $msg . ' after loop mem: '. mem() .'<br>'; }); test('foreach ref', function ($msg) use ($array) { echo $msg . ' before loop mem: '. mem() .'<br>'; foreach ($array as &$v) { $b = $v; } unset($v); echo $msg . ' after loop mem: '. mem() .'<br>'; }); test('for', function ($msg) use ($array) { echo $msg . ' before loop mem: '. mem() .'<br>'; $so = count($array); for ($i = 0; $i < $so; ++$i) { $b = $array[$i]; } echo $msg . ' after loop mem: '. mem() .'<br>'; }); test('while each -> list', function ($msg) use ($array) { echo $msg . ' before loop mem: '. mem() .'<br>'; while (list($k, $v) = each($array)) { $b =$v; } echo $msg . ' after loop mem: '. mem() .'<br>'; });
Output for 7.1.0
foreach before loop mem: 18.00 Mb<br>foreach after loop mem: 18.00 Mb<br>foreach: <b>0.002803</b><br>foreach ref before loop mem: 18.00 Mb<br>foreach ref after loop mem: 40.01 Mb<br>foreach ref: <b>0.010074</b><br>for before loop mem: 24.00 Mb<br>for after loop mem: 24.00 Mb<br>for: <b>0.003851</b><br>while each -> list before loop mem: 24.00 Mb<br>while each -> list after loop mem: 40.01 Mb<br>while each -> list: <b>-0.039865</b><br>
Output for 7.0.14
foreach before loop mem: 18.00 Mb<br>foreach after loop mem: 18.00 Mb<br>foreach: <b>0.004982</b><br>foreach ref before loop mem: 18.00 Mb<br>foreach ref after loop mem: 40.01 Mb<br>foreach ref: <b>0.016292</b><br>for before loop mem: 24.00 Mb<br>for after loop mem: 24.00 Mb<br>for: <b>0.007507</b><br>while each -> list before loop mem: 24.00 Mb<br>while each -> list after loop mem: 40.01 Mb<br>while each -> list: <b>0.072158</b><br>
Output for 7.0.6
foreach before loop mem: 18.00 Mb<br>foreach after loop mem: 18.00 Mb<br>foreach: <b>0.003074</b><br>foreach ref before loop mem: 18.00 Mb<br>foreach ref after loop mem: 40.01 Mb<br>foreach ref: <b>0.012911</b><br>for before loop mem: 24.00 Mb<br>for after loop mem: 24.00 Mb<br>for: <b>0.004570</b><br>while each -> list before loop mem: 24.00 Mb<br>while each -> list after loop mem: 40.01 Mb<br>while each -> list: <b>0.052876</b><br>
Output for 7.0.5
foreach before loop mem: 18.00 Mb<br>foreach after loop mem: 18.00 Mb<br>foreach: <b>0.004675</b><br>foreach ref before loop mem: 18.00 Mb<br>foreach ref after loop mem: 40.01 Mb<br>foreach ref: <b>0.018998</b><br>for before loop mem: 24.00 Mb<br>for after loop mem: 24.00 Mb<br>for: <b>0.006283</b><br>while each -> list before loop mem: 24.00 Mb<br>while each -> list after loop mem: 40.01 Mb<br>while each -> list: <b>0.061691</b><br>
Output for 7.0.4
foreach before loop mem: 18.00 Mb<br>foreach after loop mem: 18.00 Mb<br>foreach: <b>0.004324</b><br>foreach ref before loop mem: 18.00 Mb<br>foreach ref after loop mem: 40.01 Mb<br>foreach ref: <b>0.017178</b><br>for before loop mem: 24.00 Mb<br>for after loop mem: 24.00 Mb<br>for: <b>0.009280</b><br>while each -> list before loop mem: 24.00 Mb<br>while each -> list after loop mem: 40.01 Mb<br>while each -> list: <b>0.051192</b><br>
Output for 7.0.3
foreach before loop mem: 18.00 Mb<br>foreach after loop mem: 18.00 Mb<br>foreach: <b>0.003780</b><br>foreach ref before loop mem: 18.00 Mb<br>foreach ref after loop mem: 40.01 Mb<br>foreach ref: <b>0.027482</b><br>for before loop mem: 24.00 Mb<br>for after loop mem: 24.00 Mb<br>for: <b>0.005870</b><br>while each -> list before loop mem: 24.00 Mb<br>while each -> list after loop mem: 40.01 Mb<br>while each -> list: <b>0.063246</b><br>
Output for 7.0.2
foreach before loop mem: 18.00 Mb<br>foreach after loop mem: 18.00 Mb<br>foreach: <b>0.004224</b><br>foreach ref before loop mem: 18.00 Mb<br>foreach ref after loop mem: 40.01 Mb<br>foreach ref: <b>0.030029</b><br>for before loop mem: 24.00 Mb<br>for after loop mem: 24.00 Mb<br>for: <b>0.005168</b><br>while each -> list before loop mem: 24.00 Mb<br>while each -> list after loop mem: 40.01 Mb<br>while each -> list: <b>0.065063</b><br>
Output for 7.0.1
foreach before loop mem: 18.00 Mb<br>foreach after loop mem: 18.00 Mb<br>foreach: <b>0.003002</b><br>foreach ref before loop mem: 18.00 Mb<br>foreach ref after loop mem: 40.01 Mb<br>foreach ref: <b>0.022217</b><br>for before loop mem: 24.00 Mb<br>for after loop mem: 24.00 Mb<br>for: <b>0.004463</b><br>while each -> list before loop mem: 24.00 Mb<br>while each -> list after loop mem: 40.01 Mb<br>while each -> list: <b>0.058661</b><br>
Output for 7.0.0
foreach before loop mem: 18.00 Mb<br>foreach after loop mem: 18.00 Mb<br>foreach: <b>0.003016</b><br>foreach ref before loop mem: 18.00 Mb<br>foreach ref after loop mem: 40.01 Mb<br>foreach ref: <b>0.011396</b><br>for before loop mem: 24.00 Mb<br>for after loop mem: 24.00 Mb<br>for: <b>0.004440</b><br>while each -> list before loop mem: 24.00 Mb<br>while each -> list after loop mem: 40.01 Mb<br>while each -> list: <b>0.050175</b><br>
Output for hhvm-3.12.0
foreach before loop mem: 8.31 Mb<br>foreach after loop mem: 8.31 Mb<br>foreach: <b>0.009032</b><br>foreach ref before loop mem: 8.31 Mb<br>foreach ref after loop mem: 20.89 Mb<br>foreach ref: <b>0.015725</b><br>for before loop mem: 8.31 Mb<br>for after loop mem: 8.31 Mb<br>for: <b>0.017615</b><br>while each -> list before loop mem: 8.31 Mb<br>while each -> list after loop mem: 16.31 Mb<br>while each -> list: <b>0.057371</b><br>
Output for hhvm-3.10.0

Process exited with code 153.
Output for 5.4.25 - 5.6.28
foreach before loop mem: 43.50 Mb<br> Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 72 bytes) in /in/2mS8o on line 23
Process exited with code 255.
Output for 5.3.0 - 5.3.28, 5.4.0 - 5.4.24
Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 32 bytes) in /in/2mS8o on line 19
Process exited with code 255.
Output for 5.3.29
foreach before loop mem: 44.00 Mb<br> Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 71 bytes) in /in/2mS8o on line 23
Process exited with code 255.
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.2.17
Parse error: syntax error, unexpected T_FUNCTION in /in/2mS8o on line 21
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_FUNCTION in /in/2mS8o on line 21
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/2mS8o on line 21
Process exited with code 255.