3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Expressions { // Prerequisites for examples below public $a = 1; public static $b = 3; function bar() {} static function baz() {} function foo() { // Prerequisites for examples below $a = 1; $b = 2; $x = [1, 2]; file_put_contents('/tmp/a.php', ''); // Examples based on php-parser expression types // Using as few variables within the expressions themselves as the parser allows, to avoid false-positives static $arrowFunction = fn() => 1; static $assign = ($a = 1); static $assignOpAdd = ($a += 1); static $assignOpAnd = ($a &= 1); static $assignOpConcat = ($a .= "b"); static $assignOpDiv = ($a /= 2); static $assignRef = ($a =& $b); static $castInt = (int)"1"; static $clone = clone (new stdClass()); static $closure = function () { return 1; }; static $closureUse = function () use ($x) {}; static $empty = empty(1); static $encapsed = "Value: $a"; static $errorSuppress = @new stdClass(); static $eval = eval('1+1;'); static $funcCall = strlen('x'); static $include = include '/tmp/a.php'; static $instanceof = new stdClass() instanceof self; static $isset = isset($x); static $list = [[$a, $b] = [1, 2]]; static $match = match (1) {1 => 'a', default => 'b'}; static $methodCall = (new Expressions())->bar(); static $nullsafeMethodCall = (new Expressions())?->bar(); static $postInc = $a++; static $print = print ''; static $shellExec = `whoami`; // expected warning due to sandbox environment static $staticCall = self::baz(); static $staticPropertyFetch = self::$b; static $variable = $a; // These are invalid but give a different error static $nullsafePropertyFetch = (new Expressions())?->a; static $propertyFetch = (new Expressions())->a; } } $expr = new Expressions(); $expr->foo();

preferences:
25.13 ms | 405 KiB | 5 Q