1.2.966. Php 7 Indirect Expression

This rule reports variable indirect expressions, that are interpreted differently in PHP 5 and PHP 7.

They should be checked, as they will behave differently between these PHP versions.

<?php

// Ambiguous expression :
$b = $$foo['bar']['baz'];
echo $b;

$foo = array('bar' => array('baz' => 'bat'));
$bat = 'PHP 5.6';

// In PHP 5, the expression above means :
$b = ${$foo['bar']['baz']};
$b = 'PHP 5.6';

$foo = 'a';
$a = array('bar' => array('baz' => 'bat'));

// In PHP 7, the expression above means :
$b = ($$foo)['bar']['baz'];
$b = 'bat';

?>

See also Changes to variable handling.

1.2.966.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.966.2. Specs

Short name

Variables/Php7IndirectExpression

Rulesets

All, CompatibilityPHP53, CompatibilityPHP54, CompatibilityPHP55, CompatibilityPHP56, CompatibilityPHP70

Exakat since

0.8.4

PHP Version

With PHP 7.0 and more recent

Severity

Major

Time To Fix

Slow (1 hour)

Precision

Very high

Available in

Entreprise Edition, Exakat Cloud