3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function createCompressedString($shorts) { $compressLen = $compressPos = $currentLen = $currentPos = 0; $inBlock = false; for ($i = 0; $i < 8; $i++) { if ($shorts[$i] === 0) { if (!$inBlock) { $inBlock = true; $currentPos = $i; } $currentLen++; } else if ($inBlock) { if ($currentLen > $compressLen) { $compressLen = $currentLen; $compressPos = $currentPos; } $inBlock = false; $currentPos = $currentLen = 0; } } if ($inBlock) { $compressLen = $currentLen; $compressPos = $currentPos; } if ($compressLen === 8) { return '::'; } if ($compressLen > 1) { $replace = $compressPos === 0 || $compressPos + $compressLen === 8 ? ['', ''] : ['']; array_splice($shorts, $compressPos, $compressLen, $replace); } return implode(':', array_map(function($short) { return $short === '' ? '' : dechex($short); }, $shorts)); } var_dump(createCompressedString([1, 0, 0, 0, 1, 2001, 23234, 21]));
Output for 5.4.0 - 7.1.0
string(16) "1::1:7d1:5ac2:15"
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/iD5ne on line 36
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 '[' in /in/iD5ne on line 36
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/iD5ne on line 36
Process exited with code 255.