3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php // test data class Foo { private $a; private $b; function __construct($a, $b) { $this->a = $a; $this->b = $b; } public function getA() { return $this->a; } public function getB() { return $this->b; } } $list = [ $foo1 = new Foo(2, "bar"), $foo2 = new Foo(5, "baz"), $foo3 = new Foo(1, "bam"), ]; // simple $isGreater = function($carry, $item) { return is_null($carry) || $item->getA() > $carry->getA() ? $item : $carry; }; $maxOfList = array_reduce($list, $isGreater); assert($maxOfList === $foo2); // reusable max() function $max = function($list, callable $extract) { $compare = function($carry, $item) use($extract) { return is_null($carry) || $extract($item) > $extract($carry) ? $item : $carry; }; return array_reduce($list, $compare); }; $extractA = function($x) { return $x->getA();}; assert($max($list, $extractA) === $foo2);
Output for 5.4.0 - 7.2.0
Output for 5.1.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/IqmVh on line 19
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/IqmVh on line 19
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/IqmVh on line 6
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/IqmVh on line 6
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/IqmVh on line 6
Process exited with code 255.