3v4l.org

run code in 300+ PHP versions simultaneously
<?php function pack_encode($format, $val) { $b64 = base64_encode(pack($format, $val)); return str_replace(array('/', '='), array('_', ''), $b64); // "/" json-encodes poorly and "=" is padding } function pack_decode($format, $width, $bin) { $b64 = str_replace('_', '/', $bin); // un-padded $data = array_values(unpack($format, base64_decode($b64, false))); $val = 0; for ($i=0; $i<length($data); $i) { $val = ($val<<$width) | $data[$i]; } return $val; } function pack_encode_uint16($val) { return pack_encode('n', $val); } function pack_encode_uint32($val) { return pack_encode('N', $val); } function pack_decode_uint16($val) { return pack_decode('n', $val); } function pack_decode_uint32($val) { return pack_decode('N', $val); } $max = 32767; $n = 20; $step = intval($max/$n); for ($i=0; $i<=$n; $i++) { $val = $i*$step; $enc16 = pack_encode_uint16($val); $enc32 = pack_encode_uint32($val); $str = base64_encode(strval($val)); // STUPID echo "$enc16 $enc32 $str $val"; $dec16 = pack_decode_uint16($enc16); $dec32 = pack_decode_uint32($enc32); if ($dec16 != $val) { echo " $dec16 != $val"; } else if ($dec32 != $val) { echo " $dec32 != $val"; } echo "\n"; }
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
AAA AAAAAA MA== 0 Fatal error: Uncaught ArgumentCountError: Too few arguments to function pack_decode(), 2 passed in /in/TApNL on line 27 and exactly 3 expected in /in/TApNL:8 Stack trace: #0 /in/TApNL(27): pack_decode('n', 'AAA') #1 /in/TApNL(45): pack_decode_uint16('AAA') #2 {main} thrown in /in/TApNL on line 8
Process exited with code 255.
Output for 7.0.0 - 7.0.33
AAA AAAAAA MA== 0 Warning: Missing argument 3 for pack_decode(), called in /in/TApNL on line 27 and defined in /in/TApNL on line 8 Notice: Undefined variable: bin in /in/TApNL on line 9 Fatal error: Uncaught Error: Call to undefined function  array_values() in /in/TApNL:10 Stack trace: #0 /in/TApNL(27): pack_decode('n', 'AAA') #1 /in/TApNL(45): pack_decode_uint16('AAA') #2 {main} thrown in /in/TApNL on line 10
Process exited with code 255.
Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40
AAA AAAAAA MA== 0 Warning: Missing argument 3 for pack_decode(), called in /in/TApNL on line 27 and defined in /in/TApNL on line 8 Notice: Undefined variable: bin in /in/TApNL on line 9 Fatal error: Call to undefined function  array_values() in /in/TApNL on line 10
Process exited with code 255.
Output for 5.0.0 - 5.0.5
AAA AAAAAA MA== 0 Warning: Missing argument 3 for pack_decode() in /in/TApNL on line 8 Fatal error: Call to undefined function  array_values() in /in/TApNL on line 10
Process exited with code 255.
Output for 4.4.5 - 4.4.9
AAA AAAAAA MA== 0 Warning: Missing argument 3 for pack_decode() in /in/TApNL on line 8 Fatal error: Call to undefined function:  array_values() in /in/TApNL on line 10
Process exited with code 255.
Output for 4.3.2 - 4.3.11, 4.4.0 - 4.4.4
AAA AAAAAA MA== 0 Warning: Missing argument 3 for pack_decode() in /in/TApNL on line 8 Fatal error: Call to undefined function:  array_values() in /in/TApNL on line 10
Process exited with code 255.
Output for 4.3.0 - 4.3.1
AAA AAAAAA MA== 0 Warning: Missing argument 3 for pack_decode() in /in/TApNL on line 8 Fatal error: Call to undefined function:  array_values() in /in/TApNL on line 10

preferences:
302.73 ms | 402 KiB | 459 Q