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 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
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
Output for 7.1.25 - 7.1.32, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
isset->a: a isset->b: false isset[a]: a isset[b]: false [a]??false: a [b]??false: false ->a??false: a ->b??false: false

preferences:
115.48 ms | 1515 KiB | 4 Q