3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $digits = array(0,1,2,3,4,5,6,7,7,8,9,'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'); $base = count($digits); function enc($val) use($digits, $base) { $str = ''; if (0 === $val) { return '0'; } if (0 > $val) { $str = '-'; $val = -$val; } while (0 < $val) { $str .= $digits[$val % $base]; $val = (int)($val / $base); } return $str; } function gdr_encode($val) { $bin = ''; while ($val > 0) { $bin .= chr($val & 0xFF); $val >>= 8; } return str_replace(array('/', '='), array('_', ''), base64_encode($bin)); } function gdr_decode($str) { $bin = base64_decode(str_replace('_', '/', $str)); $val = 0; for ($i=strlen($bin)-1; $i>=0; $i--) { $val |= ord($bin[$i]) << $i*8; } return $val; } if (!function_exists('json_encode')) { // crappy json_encode function that can handle our narrow input function json_encode($val) { if (is_string($val)) { return "\"$val\""; } if (is_int($val)) { return strval($val); } return "???"; } } $vals = array(0, 1, 99, 100, 255, 256, 999, 1000, 9999, 10000, 65535, 65536, 99999, 100000, 999999, 1000000, 4294967295, 4294967296); foreach ($vals as $val) { $enc = json_encode(gdr_encode($val)); $dec = json_encode(gdr_decode($enc)); $val = json_encode($val); $enc2 = json_encode(enc($val)); if (strlen($enc) > strlen($val)) { echo "$enc $val $enc2 WORSE\n"; } else if (strlen($enc) == strlen($val)) { echo "$enc $val $enc2 EQUAL\n"; } else { echo "$enc $val $enc2\n"; } }
based on qPTR5
Output for 5.4.0 - 5.4.31
Parse error: syntax error, unexpected 'use' (T_USE), expecting '{' in /in/XaOEq on line 6
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_USE, expecting '{' in /in/XaOEq on line 6
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_USE, expecting '{' in /in/XaOEq on line 6
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `'{'' in /in/XaOEq on line 6
Process exited with code 255.