3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function lzw_compress($string) { // compression $dictionary = array_flip(range("\0", "\xFF")); $word = ""; $codes = array(); for ($i=0; $i <= strlen($string); $i++) { $x = substr($string, $i, 1); if (strlen($x) && isset($dictionary[$word . $x])) { $word .= $x; } elseif ($i) { $codes[] = $dictionary[$word]; $dictionary[$word . $x] = count($dictionary); $word = $x; } } // convert codes to binary string $dictionary_count = 256; $bits = 8; // ceil(log($dictionary_count, 2)) $return = ""; $rest = 0; $rest_length = 0; foreach ($codes as $code) { $rest = ($rest << $bits) + $code; $rest_length += $bits; $dictionary_count++; if ($dictionary_count >> $bits) { $bits++; } while ($rest_length > 7) { $rest_length -= 8; $return .= chr($rest >> $rest_length); $rest &= (1 << $rest_length) - 1; } } return $return . ($rest_length ? chr($rest << (8 - $rest_length)) : ""); } /** LZW decompression * @param string compressed binary data * @return string original data */ function lzw_decompress($binary) { $dictionary_count = 256; $bits = 8; $codes = array(); $rest = 0; $rest_length = 0; mb_internal_encoding("UTF-8"); for ($i = 0; $i < mb_strlen($binary); $i++ ) {$codes[] = mb_ord(mb_substr($binary, $i, 1)); } // decompression $dictionary = range("\0", "\xFF"); $return = ""; foreach ($codes as $i => $code) { $element = $dictionary[$code]; if (!isset($element)) $element = $word . $word[0]; $return .= $element; if ($i) $dictionary[] = $word . $element[0]; $word = $element; } return $return; } function mb_ord($string) { if (extension_loaded('mbstring') === true) { mb_language('Neutral'); mb_internal_encoding('UTF-8'); mb_detect_order(array('UTF-8', 'ISO-8859-15', 'ISO-8859-1', 'ASCII')); $result = unpack('N', mb_convert_encoding($string, 'UCS-4BE', 'UTF-8')); if (is_array($result) === true) return $result[1]; } return ord($string); } lzw_decompress(lzw_compress('function kw($n){return $n*$n} echo kw(5);'));
Output for 5.4.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
Output for 4.3.5 - 5.3.29
Warning: unpack(): Type N: not enough input, need 4, have 0 in /in/DOmKv on line 72 Warning: unpack(): Type N: not enough input, need 4, have 0 in /in/DOmKv on line 72 Warning: unpack(): Type N: not enough input, need 4, have 0 in /in/DOmKv on line 72 Warning: unpack(): Type N: not enough input, need 4, have 0 in /in/DOmKv on line 72 Warning: unpack(): Type N: not enough input, need 4, have 0 in /in/DOmKv on line 72 Warning: unpack(): Type N: not enough input, need 4, have 0 in /in/DOmKv on line 72 Warning: unpack(): Type N: not enough input, need 4, have 0 in /in/DOmKv on line 72 Warning: unpack(): Type N: not enough input, need 4, have 0 in /in/DOmKv on line 72 Warning: unpack(): Type N: not enough input, need 4, have 0 in /in/DOmKv on line 72 Warning: unpack(): Type N: not enough input, need 4, have 0 in /in/DOmKv on line 72
Output for 4.3.2 - 4.3.4
Notice: Undefined offset: 1 in /in/DOmKv on line 73 Notice: Undefined offset: 1 in /in/DOmKv on line 73 Warning: unpack(): Type N: not enough input, need 4, have 0 in /in/DOmKv on line 72 Warning: unpack(): Type N: not enough input, need 4, have 0 in /in/DOmKv on line 72 Notice: Undefined offset: 1 in /in/DOmKv on line 73 Notice: Undefined offset: 1 in /in/DOmKv on line 73 Notice: Undefined offset: 1 in /in/DOmKv on line 73 Warning: unpack(): Type N: not enough input, need 4, have 0 in /in/DOmKv on line 72 Notice: Undefined offset: 1 in /in/DOmKv on line 73 Notice: Undefined offset: 1 in /in/DOmKv on line 73 Warning: unpack(): Type N: not enough input, need 4, have 0 in /in/DOmKv on line 72 Notice: Undefined offset: 1 in /in/DOmKv on line 73 Notice: Undefined offset: 1 in /in/DOmKv on line 73 Notice: Undefined offset: 1 in /in/DOmKv on line 73 Warning: unpack(): Type N: not enough input, need 4, have 0 in /in/DOmKv on line 72 Notice: Undefined offset: 1 in /in/DOmKv on line 73 Notice: Undefined offset: 1 in /in/DOmKv on line 73 Warning: unpack(): Type N: not enough input, need 4, have 0 in /in/DOmKv on line 72 Notice: Undefined offset: 1 in /in/DOmKv on line 73 Notice: Undefined offset: 1 in /in/DOmKv on line 73 Notice: Undefined offset: 1 in /in/DOmKv on line 73 Notice: Undefined offset: 1 in /in/DOmKv on line 73 Notice: Undefined offset: 1 in /in/DOmKv on line 73 Notice: Undefined offset: 1 in /in/DOmKv on line 73 Warning: unpack(): Type N: not enough input, need 4, have 0 in /in/DOmKv on line 72 Notice: Undefined offset: 1 in /in/DOmKv on line 73 Warning: unpack(): Type N: not enough input, need 4, have 0 in /in/DOmKv on line 72 Notice: Undefined offset: 1 in /in/DOmKv on line 73 Notice: Undefined offset: 1 in /in/DOmKv on line 73 Warning: unpack(): Type N: not enough input, need 4, have 0 in /in/DOmKv on line 72 Notice: Undefined offset: 1 in /in/DOmKv on line 73 Notice: Undefined offset: 1 in /in/DOmKv on line 73 Notice: Undefined offset: 1 in /in/DOmKv on line 73 Warning: unpack(): Type N: not enough input, need 4, have 0 in /in/DOmKv on line 72 Notice: Undefined offset: 1 in /in/DOmKv on line 73 Notice: Undefined offset: 1 in /in/DOmKv on line 73 Notice: Undefined index: in /in/DOmKv on line 58 Notice: Undefined variable: word in /in/DOmKv on line 59 Notice: Undefined variable: word in /in/DOmKv on line 59 Notice: Undefined index: in /in/DOmKv on line 58 Notice: Uninitialized string offset: 0 in /in/DOmKv on line 59 Notice: Uninitialized string offset: 0 in /in/DOmKv on line 61 Notice: Undefined index: in /in/DOmKv on line 58 Notice: Undefined index: in /in/DOmKv on line 58 Notice: Undefined index: in /in/DOmKv on line 58 Notice: Undefined index: in /in/DOmKv on line 58 Notice: Undefined index: in /in/DOmKv on line 58 Notice: Undefined index: in /in/DOmKv on line 58 Notice: Undefined index: in /in/DOmKv on line 58 Notice: Undefined index: in /in/DOmKv on line 58 Notice: Undefined index: in /in/DOmKv on line 58 Notice: Undefined index: in /in/DOmKv on line 58 Notice: Undefined index: in /in/DOmKv on line 58 Notice: Undefined index: in /in/DOmKv on line 58 Notice: Undefined index: in /in/DOmKv on line 58 Notice: Undefined index: in /in/DOmKv on line 58 Notice: Undefined index: in /in/DOmKv on line 58 Notice: Undefined index: in /in/DOmKv on line 58 Notice: Undefined index: in /in/DOmKv on line 58 Notice: Undefined index: in /in/DOmKv on line 58 Notice: Undefined index: in /in/DOmKv on line 58 Notice: Undefined index: in /in/DOmKv on line 58 Notice: Undefined index: in /in/DOmKv on line 58 Notice: Undefined index: in /in/DOmKv on line 58 Notice: Undefined index: in /in/DOmKv on line 58 Notice: Undefined index: in /in/DOmKv on line 58
Output for 4.3.0 - 4.3.1

Process exited with code 137.