Uniform variable syntax in PHP 7

In the older versions of PHP, we faced an inconsistency problem. For example: ${$first [‘name’]}. This syntax may create confusion or we can say that the syntax is not consistent. To overcome the inconsistency problem, PHP 7 added the new syntax called “Uniform variable syntax”.

The uniform variable syntax evaluates the variables from left to right.We need to add the curly brackets to use the uniform variable syntax. For example,

echo ${$first[‘name’]};

The uniform variable syntax allows the combinations of operators and also it can break the backward compatibility in some expressions wherever older evaluations are used.


Live Demo

   $x = (function() {
      return 20 - 10;
   echo "$x
"; ?>


Output for the above PHP program will be:


Note: The above program will immediately invoke the function expression.

The uniform variable syntax uses new combinations of existing syntax. For example,


The uniform variable syntax can dereference the characters in the strings returned by functions.

[$obj, $obj1] [0]->pro;

In some cases, PHP 7 supports the nested double colons(::),


Nested Method/function calls

We can use the nested method and function calls or any callable to doubling up the parentheses.


foo()(); //return by a function callable
$foo->bar()(); // return by an instance method
Foo::bar()(); // static method
$foo()(); // return by another callable

Arbitrary Expression Dereferencing

In PHP we can now dereference any valid expression including with the parentheses. For example,

(exp) [‘foo’] ; // It will access an array key
(exp)->foo; // This will access the property
(exp)->foo(); // It will call to a method etc.


Live Demo

   function emp() {
      echo "This is emp() 
";    };    function dept() {       echo "This is dept()
";       return emp;    };    function sub() {       echo "This is sub()
";       return dept;    };    sub();    echo "----------------
";    sub()();    echo "----------------
";    sub()()(); ?>


The output for the above program will be −

This is sub()
This is sub()
This is dept()
This is sub()
This is dept()
This is emp()