1.2.953. PHP5 Indirect Variable Expression

Indirect variable expressions changes between PHP 5 an 7.

The following structures are evaluated differently in PHP 5 and 7. It is recommended to review them or switch to a less ambiguous syntax. +———————–+————————-+————————-+ | Expression | PHP 5 interpretation | PHP 7 interpretation | +———————–+————————-+————————-+ |$$foo[‘bar’][‘baz’] |${$foo[‘bar’][‘baz’]} |($$foo)[‘bar’][‘baz’] | |$foo->$bar[‘baz’] |$foo->{$bar[‘baz’]} |($foo->$bar)[‘baz’] | |$foo->$bar[‘baz’]() |$foo->{$bar[‘baz’]}() |($foo->$bar)[‘baz’]() | |Foo::$bar[‘baz’]() |Foo::{$bar[‘baz’]}() |(Foo::$bar)[‘baz’]() | +———————–+————————-+————————-+

<?php

// PHP 7
$foo = 'bar';
$bar['bar']['baz'] = 'foobarbarbaz';
echo $$foo['bar']['baz'];
echo ($$foo)['bar']['baz'];

// PHP 5
$foo['bar']['baz'] = 'bar';
$bar = 'foobarbazbar';
echo $$foo['bar']['baz'];
echo $\{$foo['bar']['baz']\};

?>

See also Backward incompatible changes PHP 7.0.

1.2.953.1. Connex PHP features

1.2.953.1.1. Suggestions

  • Avoid using complex expressions, mixing $$\, [0] and -> in the same expression

  • Add curly braces {} to ensure that the precedence is the same between PHP 5 and 7. For example, $$v becomes $\{$v\}

1.2.953.1.2. Specs

Short name

Variables/Php5IndirectExpression

Rulesets

All, Changed Behavior, CompatibilityPHP53, CompatibilityPHP54, CompatibilityPHP55, CompatibilityPHP56

Exakat since

0.8.4

PHP Version

With PHP 7.0 and older

Severity

Major

Time To Fix

Slow (1 hour)

Precision

Very high

Available in

Entreprise Edition, Exakat Cloud