3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?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 cmp($val) { return strlen(gdr_encode($val))+2-strlen($val); } function insert(&$vals, $n) { $min = intval('1'.str_repeat('0', $n-1)); $max = intval(str_repeat('9', $n)); $vals[] = $min; $vals[] = $max; $cmp_min = cmp($min); $cmp_max = cmp($max); if ($cmp_min === $cmp_max) { return; } if ($cmp_min+1 !== $cmp_max) { echo "BAD SEARCH ASSUMPTION: [$min => $cmp_min, $max => $cmp_max]\n"; } while ($min <= $max) { $mid = (int)(($min+$max)/2); $cmp_mid = cmp($mid); echo "$min=>$cmp_min $mid=>$cmp_mid $max=>$cmp_max\n"; if ($cmp_min === $cmp_mid && $cmp_min+1 !== cmp($mid+1)) { $vals[] = $mid; $vals[] = $mid+1; return; } if ($min === $max) { return; } if ($cmp_min < $cmp_mid) { $max = $mid-1; } else { $min = $mid+1; } } } $vals = array(0); for ($i=1; $i<8; $i++) { insert($vals, $i); } $vals = array_unique($vals); sort($vals); 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), cmp($val)); } }
based on Om3dd
Output for 5.4.0 - 5.4.31
Parse error: syntax error, unexpected '$cmp_min' (T_VARIABLE) in /in/Klor4 on line 68
Process exited with code 255.
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.3.29
Parse error: syntax error, unexpected T_VARIABLE in /in/Klor4 on line 68
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_VARIABLE in /in/Klor4 on line 68
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/Klor4 on line 68
Process exited with code 255.