1.2.45. Array With String Initialization

It used to be possible to initialize a variable with an string, and use it as an array. It is not the case anymore in PHP 7.1.

<?php

// Initialize arrays with array()
$a = array();
$a[3] = "4";

// Don't start with a string
$a = '';
$a[3] = "4";
print $a;

// Don't start with a string
if (is_numeric($a)) {
    $a[] = $a;
}

?>

See also PHP 7.1 no longer converts string to arrays the first time a value is assigned with square bracket notation.

1.2.45.1. Suggestions

  • Always initialize arrays with an empty array(), not a string.

1.2.45.2. Specs

Short name

Arrays/StringInitialization

Rulesets

All, CompatibilityPHP71

Exakat since

1.6.5

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Available in

Entreprise Edition, Exakat Cloud