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>'. PHP_EOL; } $array = range(0, 300000); test('foreach', function ($msg) use ($array) { echo $msg . ' before loop mem: '. mem() .PHP_EOL; foreach ($array as $v) { $b = $v; } echo $msg . ' after loop mem: '. mem() .PHP_EOL; }); test('foreach ref', function ($msg) use ($array) { echo $msg . ' before loop mem: '. mem() .PHP_EOL; foreach ($array as &$v) { $b = $v; } unset($v); echo $msg . ' after loop mem: '. mem() .PHP_EOL; }); test('for', function ($msg) use ($array) { echo $msg . ' before loop mem: '. mem() .PHP_EOL; $so = count($array); for ($i = 0; $i < $so; ++$i) { $b = $array[$i]; } echo $msg . ' after loop mem: '. mem() .PHP_EOL; }); test('while each -> list', function ($msg) use ($array) { echo $msg . ' before loop mem: '. mem() .PHP_EOL; while (list($k, $v) = each($array)) { $b =$v; } echo $msg . ' after loop mem: '. mem() .PHP_EOL; }); echo 'Memory peak: ' . round(memory_get_peak_usage() / 1048576, 3) .PHP_EOL;
Output for 7.1.0
foreach before loop mem: 18.00 Mb foreach after loop mem: 18.00 Mb foreach: <b>0.004830</b> foreach ref before loop mem: 18.00 Mb foreach ref after loop mem: 40.01 Mb foreach ref: <b>0.015929</b> for before loop mem: 24.00 Mb for after loop mem: 24.00 Mb for: <b>0.006290</b> while each -> list before loop mem: 24.00 Mb while each -> list after loop mem: 40.01 Mb while each -> list: <b>0.060960</b> Memory peak: 39.218
Output for 7.0.14
foreach before loop mem: 18.00 Mb foreach after loop mem: 18.00 Mb foreach: <b>0.005197</b> foreach ref before loop mem: 18.00 Mb foreach ref after loop mem: 40.01 Mb foreach ref: <b>0.015991</b> for before loop mem: 24.00 Mb for after loop mem: 24.00 Mb for: <b>0.007370</b> while each -> list before loop mem: 24.00 Mb while each -> list after loop mem: 40.01 Mb while each -> list: <b>0.071568</b> Memory peak: 39.219
Output for 7.0.6
foreach before loop mem: 18.00 Mb foreach after loop mem: 18.00 Mb foreach: <b>0.004601</b> foreach ref before loop mem: 18.00 Mb foreach ref after loop mem: 40.01 Mb foreach ref: <b>0.015244</b> for before loop mem: 24.00 Mb for after loop mem: 24.00 Mb for: <b>0.005828</b> while each -> list before loop mem: 24.00 Mb while each -> list after loop mem: 40.01 Mb while each -> list: <b>0.057000</b> Memory peak: 39.219
Output for 7.0.5
foreach before loop mem: 18.00 Mb foreach after loop mem: 18.00 Mb foreach: <b>0.003222</b> foreach ref before loop mem: 18.00 Mb foreach ref after loop mem: 40.01 Mb foreach ref: <b>0.012752</b> for before loop mem: 24.00 Mb for after loop mem: 24.00 Mb for: <b>0.004480</b> while each -> list before loop mem: 24.00 Mb while each -> list after loop mem: 40.01 Mb while each -> list: <b>0.050305</b> Memory peak: 39.219
Output for 7.0.4
foreach before loop mem: 18.00 Mb foreach after loop mem: 18.00 Mb foreach: <b>0.003959</b> foreach ref before loop mem: 18.00 Mb foreach ref after loop mem: 40.01 Mb foreach ref: <b>0.014259</b> for before loop mem: 24.00 Mb for after loop mem: 24.00 Mb for: <b>0.005112</b> while each -> list before loop mem: 24.00 Mb while each -> list after loop mem: 40.01 Mb while each -> list: <b>0.052008</b> Memory peak: 39.219
Output for 7.0.3
foreach before loop mem: 18.00 Mb foreach after loop mem: 18.00 Mb foreach: <b>0.003201</b> foreach ref before loop mem: 18.00 Mb foreach ref after loop mem: 40.01 Mb foreach ref: <b>0.022157</b> for before loop mem: 24.00 Mb for after loop mem: 24.00 Mb for: <b>0.004443</b> while each -> list before loop mem: 24.00 Mb while each -> list after loop mem: 40.01 Mb while each -> list: <b>0.059699</b> Memory peak: 39.219
Output for 7.0.2
foreach before loop mem: 18.00 Mb foreach after loop mem: 18.00 Mb foreach: <b>0.003257</b> foreach ref before loop mem: 18.00 Mb foreach ref after loop mem: 40.01 Mb foreach ref: <b>0.020583</b> for before loop mem: 24.00 Mb for after loop mem: 24.00 Mb for: <b>0.004461</b> while each -> list before loop mem: 24.00 Mb while each -> list after loop mem: 40.01 Mb while each -> list: <b>0.057393</b> Memory peak: 39.219
Output for 7.0.1
foreach before loop mem: 18.00 Mb foreach after loop mem: 18.00 Mb foreach: <b>0.003281</b> foreach ref before loop mem: 18.00 Mb foreach ref after loop mem: 40.01 Mb foreach ref: <b>0.011288</b> for before loop mem: 24.00 Mb for after loop mem: 24.00 Mb for: <b>0.004683</b> while each -> list before loop mem: 24.00 Mb while each -> list after loop mem: 40.01 Mb while each -> list: <b>0.052512</b> Memory peak: 39.219
Output for 7.0.0
foreach before loop mem: 18.00 Mb foreach after loop mem: 18.00 Mb foreach: <b>0.004251</b> foreach ref before loop mem: 18.00 Mb foreach ref after loop mem: 40.01 Mb foreach ref: <b>0.014619</b> for before loop mem: 24.00 Mb for after loop mem: 24.00 Mb for: <b>0.005422</b> while each -> list before loop mem: 24.00 Mb while each -> list after loop mem: 40.01 Mb while each -> list: <b>0.055496</b> Memory peak: 39.219
Output for 5.4.0 - 5.6.28
foreach before loop mem: 43.50 Mb Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 72 bytes) in /in/DHFsg on line 23
Process exited with code 255.
Output for 5.3.0 - 5.3.29
foreach before loop mem: 44.00 Mb Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 71 bytes) in /in/DHFsg 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/DHFsg 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/DHFsg on line 21
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/DHFsg on line 21
Process exited with code 255.