3v4l.org

run code in 300+ PHP versions simultaneously
<?php function gdr_encode($val) { // all characters that json encode without escaping $chrs = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 !#$%&\'()*+,-.:;<=>?@[]^_`{|}~'; $base = strlen($chrs); $str = ''; while ($val > 0) { $str = $chrs[$val % $base] . $str; $val = (int)($val / $base); } return $str; } function gdr_decode($str) { // all characters that json encode without escaping $chrs = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 !#$%&\'()*+,-.:;<=>?@[]^_`{|}~'; $base = strlen($chrs); $map = array(); for ($i=0; $i<$base; $i++) { $map[$chrs[$i]] = $i; } $val = 0; for ($i=0; $i<strlen($str); $i++) { $val *= $base; $val += (int)$map[$str[$i]]; } return $val; } function duration($val) { if ($val < 24) { return sprintf('%d', $val).'h'; } if ($val < 24*10) { return sprintf('%1.1f', $val/24).'d'; } if ($val < 168*10) { return sprintf('%1.1f', $val/168).'w'; } //if ($val < 730*10) { // return sprintf('%1.1f', $val/730).'m'; //} return sprintf('%1.1f', $val/8760).'y'; } function search($min, $max, $k){ if ($max < $min) { return null; } $mid = round(($min+$max)/2, 0)-1; $len = strlen(gdr_encode($mid)); if ($len == $k && strlen(gdr_encode($mid+1)) == $k+1) { return $mid; } if ($len < $k) { return search($min, $mid-1, $k); } return search($mid+1, $max, $k); } function insert(&$vals, $n) { $min = intval('1'.str_repeat('0', $n-1)); $max = intval(str_repeat('9', $n)); $vals[] = $min; $vals[] = $max; $len = strlen(gdr_encode($min)); if ($len == strlen(gdr_encode($max))) { return; } $mid = search($min, $max, $len); if ($mid === null) { return; } $vals[] = $mid; $vals[] = $mid+1; } $vals = array(0); for ($i=1; $i<8; $i++) { insert($vals, $i); } $vals = array_unique($vals); sort($vals); //$vals = array( // 0, 1, 99, 100, 255, 256, 999, 1000, 9999, 10000, 65535, 65536, 99999, // 100000, 250000, 500000, 750000, 999999, 1000000, 9999999, //4294967295, 4294967296 //); echo sprintf("%7s %7s %7s %4s\n", 'num', 'str', 'time', 'cmp'); foreach ($vals as $val) { $enc = gdr_encode($val); $dec = gdr_decode($enc); if ($dec !== $val) { echo "ERROR: $val => $enc => $dec\n"; } else { echo sprintf("%7d %7s %7s %4d\n", $dec, "\"$enc\"", duration($val), strlen($enc)+2-strlen($val)); } }
Output for 7.0.0 - 7.0.20, 7.4.3 - 7.4.33, 8.0.0 - 8.0.12, 8.0.14 - 8.0.30, 8.1.0 - 8.1.1, 8.1.28, 8.2.18, 8.3.5 - 8.3.6
Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 262144 bytes) in /in/T6Nvb on line 51
Process exited with code 255.
Output for 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
Fatal error: Out of memory (allocated 31457280 bytes) (tried to allocate 262144 bytes) in /in/T6Nvb on line 51 mmap() failed: [12] Cannot allocate memory
Process exited with code 255.
Output for 8.1.2 - 8.1.27
Fatal error: Out of memory (allocated 31457280) (tried to allocate 262144 bytes) in /in/T6Nvb on line 51 mmap() failed: [12] Cannot allocate memory
Process exited with code 255.
Output for 8.0.13
Fatal error: Out of memory (allocated 130023424) (tried to allocate 262144 bytes) in /in/T6Nvb on line 51 mmap() failed: [12] Cannot allocate memory mmap() failed: [12] Cannot allocate memory
Process exited with code 255.
Output for 7.4.0
Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 262144 bytes) in /in/T6Nvb on line 10
Process exited with code 255.
Output for 7.3.32 - 7.3.33
Fatal error: Out of memory (allocated 130023424) (tried to allocate 262144 bytes) in /in/T6Nvb on line 48 mmap() failed: [12] Cannot allocate memory mmap() failed: [12] Cannot allocate memory
Process exited with code 255.
Output for 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.31
Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 262144 bytes) in /in/T6Nvb on line 48
Process exited with code 255.
Output for 5.4.0 - 5.4.45, 5.6.8 - 5.6.28
Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 130968 bytes) in /in/T6Nvb on line 52
Process exited with code 255.
Output for 5.5.24 - 5.5.35
Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 32 bytes) in /in/T6Nvb on line 7
Process exited with code 255.
Output for 5.3.0 - 5.3.29
Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 523800 bytes) in /in/T6Nvb on line 52
Process exited with code 255.
Output for 4.3.0 - 4.3.11, 4.4.0 - 4.4.9, 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17

Process exited with code 139.

preferences:
236.43 ms | 402 KiB | 312 Q