3v4l.org

run code in 500+ PHP versions simultaneously
<?php function twig_compare($a, $b) { // int <=> string if (\is_int($a) && \is_string($b)) { $b = trim($b); if (!is_numeric($b)) { return (string) $a <=> $b; } if ((int) $b == $b) { return $a <=> (int) $b; } else { return (float) $a <=> (float) $b; } } throw new \LogicException('partial implementation for quick test'); } $failures = []; foreach ([ [1, 42, ' foo'], [0, 42, "42\f"], [1, 42, "\x00\x34\x32"], ] as [$expected, $a, $b]) { $actual = twig_compare($a, $b); if ($expected === $actual) { echo '.'; } else { echo 'F'; $failures[] = [$a, $b, $expected, $actual]; } } echo "\n"; if (!$failures) { echo "OK\n"; } else { echo count($failures)." test(s) failed:\n"; foreach ($failures as [$a, $b, $expected, $actual]) { echo '('.var_export($a, true).', '.var_export($b, true)."): expected $expected, actual $actual\n"; } }
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.32, 8.3.0 - 8.3.32, 8.4.1 - 8.4.23, 8.5.0 - 8.5.8
F.F 2 test(s) failed: (42, ' foo'): expected 1, actual -1 (42, '' . "\0" . '42'): expected 1, actual 0
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
FFF 3 test(s) failed: (42, ' foo'): expected 1, actual -1 (42, '42 '): expected 0, actual -1 (42, '' . "\0" . '42'): expected 1, actual 0

preferences:
110.79 ms | 1540 KiB | 4 Q