1.2.857. Non-constant Index In Array¶
Undefined constants revert as strings in Arrays. They are also called barewords
.
In $array[index]
, PHP cannot find index as a constant, but, as a default behavior, turns it into the string index
.
This default behavior raise concerns when a corresponding constant is defined, either using define() or the const keyword (outside a class). The definition of the index constant will modify the behavior of the index, as it will now use the constant definition, and not the ‘index’ string.
It is recommended to make index a real string (with ‘ or “), or to define the corresponding constant to avoid any future surprise.
Note that PHP 7.2 removes the support for this feature.
<?php
// assign 1 to the element index in $array
// index will fallback to string
$array[index] = 1;
//PHP Notice: Use of undefined constant index - assumed 'index'
echo $array[index]; // display 1 and the above error
echo "$array[index]"; // display 1
echo "$array['index']"; // Syntax error
define('index', 2);
// now 1 to the element 2 in $array
$array[index] = 1;
?>
See also PHP RFC: Deprecate and Remove Bareword (Unquoted) Strings and Syntax.
1.2.857.2. Connex PHP features¶
1.2.857.2.1. Suggestions¶
Declare the constant to give it an actual value
Turn the constant name into a string
1.2.857.2.2. Specs¶
Short name |
Arrays/NonConstantArray |
Rulesets |
|
Exakat since |
0.8.4 |
PHP Version |
All |
Severity |
Minor |
Time To Fix |
Instant (5 mins) |
Precision |
High |
Examples |
|
Available in |