1.2.1452. Weird Array Index

Array index that looks weird. Arrays index may be string or integer, but some strings looks weird.

In particular, strings that include terminal white spaces, often leads to missed values. Although this is rare error, and often easy to spot, it is also very hard to find when it strikes.

<?php

$array = ['a ' => 1, 'b' => 2, 'c' => 3];

// Later in the code

//Notice: Undefined index: a in /Users/famille/Desktop/analyzeG3/test.php on line 8
echo $array['a'];

//Notice: Undefined index: b  in /Users/famille/Desktop/analyzeG3/test.php on line 10
// Note that the space is visible, but easy to miss
echo $array['b '];

// all fine here
echo $array['c'];

?>

1.2.1452.1. Connex PHP features

1.2.1452.1.1. Suggestions

  • Remove white spaces when using strings as array index.

1.2.1452.1.2. Specs

Short name

Arrays/WeirdIndex

Rulesets

All, Changed Behavior, Semantics

Exakat since

1.9.9

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Available in

Entreprise Edition, Exakat Cloud