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, 3000000); 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.0.0 - 7.1.0
Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 134217736 bytes) in /in/WsfZY on line 19
Process exited with code 255.
Output for hhvm-3.12.0
foreach before loop mem: 64.31 Mb foreach after loop mem: 64.31 Mb foreach: <b>0.086403</b> foreach ref before loop mem: 64.31 Mb mallctl opt.prof: No such file or directory (2) Fatal error: request has exceeded memory limit in /in/WsfZY on line 32
Process exited with code 255.
Output for hhvm-3.10.0
foreach before loop mem: 64.31 Mb foreach after loop mem: 64.31 Mb foreach: <b>0.121183</b> foreach ref before loop mem: 64.31 Mb Fatal error: request has exceeded memory limit in /in/WsfZY on line 32
Process exited with code 255.
Output for 5.3.0 - 5.6.28
Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 32 bytes) in /in/WsfZY on line 19
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/WsfZY 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/WsfZY on line 21
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/WsfZY on line 21
Process exited with code 255.