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, 30000000); 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 1073741832 bytes) in /in/X063o on line 19
Process exited with code 255.
Output for hhvm-3.12.0
mallctl opt.prof: No such file or directory (2) Fatal error: request has exceeded memory limit in /in/X063o on line 19
Process exited with code 255.
Output for hhvm-3.10.0
Fatal error: request has exceeded memory limit in /in/X063o on line 19
Process exited with code 255.
Output for 5.5.0 - 5.6.28
Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 32 bytes) in /in/X063o on line 19
Process exited with code 255.
Output for 5.3.0 - 5.4.45
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 32 bytes) in /in/X063o on line 19
Process exited with code 255.
Output for 5.2.3 - 5.2.17
Parse error: syntax error, unexpected T_FUNCTION in /in/X063o on line 21
Process exited with code 255.
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.2.2
<br /> <b>Parse error</b>: syntax error, unexpected T_FUNCTION in <b>/in/X063o</b> on line <b>21</b><br />
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
<br /> <b>Parse error</b>: parse error, unexpected T_FUNCTION in <b>/in/X063o</b> on line <b>21</b><br />
Process exited with code 255.
Output for 4.3.2 - 4.3.4
<br /> <b>Parse error</b>: parse error in <b>/in/X063o</b> on line <b>21</b><br />
Process exited with code 255.