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.
Example
<?php $x = (function() { return 20 - 10; }) (); echo "$x\n"; ?>
Output
Output for the above PHP program will be:
10
Note: The above program will immediately invoke the function expression.
The uniform variable syntax uses new combinations of existing syntax. For example,
$foo([‘bar’])();
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(::),
$foo[‘bar’]::$baz;
Nested Method/function calls
We can use the nested method and function calls or any callable to doubling up the parentheses.
Example
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.
Example
<?php function emp() { echo "This is emp() \n"; }; function dept() { echo "This is dept() \n"; return emp; }; function sub() { echo "This is sub()\n"; return dept; }; sub(); echo "----------------\n"; sub()(); echo "----------------\n"; sub()()(); ?>
Output
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()