3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * This class implements ArrayAccess and the magic * isset method to test the behavior of certain operators * * It simulates having just a property called a */ class Test implements ArrayAccess { public function __isset($property) { return $property === 'a'; } public function __get($property) { return $property === 'a'? 'a' : null; } public function offsetExists ( $offset ) : bool { return $offset === 'a'; } /** * We are required by the interface to implement these methods * but they're irrelevant to our test */ public function offsetGet ( $offset ) { return $offset === 'a'? 'a' : null; } public function offsetSet ($offset , $value ) : void { return; } public function offsetUnset ($offset ) : void { return; } } /** * Start performing the actual tests, we wanna compare the * behavior of isset() and the ?? operator */ $tests = [ 'isset->a' => function($e) { return isset($e->a) && $e->a? $e->a : false; }, 'isset->b' => function($e) { return isset($e->b) && $e->b? $e->b : false; }, 'isset[a]' => function($e) { return isset($e['a']) && $e['a']? $e['a'] : false; }, 'isset[b]' => function($e) { return isset($e['b']) && $e['b']? $e['b'] : false; }, '[a]??false' => function($e) { return $e['a']??false; }, '[b]??false' => function($e) { return $e['b']??false; }, '->a??false' => function($e) { return $e->a??false; }, '->b??false' => function($e) { return $e->b??false; } ]; $a = new Test(); foreach ($tests as $caption => $fn) { echo $caption, ': ', ($fn($a)? $fn($a) : 'false'), PHP_EOL; }
Output for git.master, git.master_jit, rfc.property-hooks
Deprecated: Return type of Test::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/v7YYP on line 29 isset->a: a isset->b: false isset[a]: a isset[b]: false [a]??false: a [b]??false: false ->a??false: a ->b??false: false

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
38.95 ms | 1542 KiB | 4 Q