1.2.882. One Variable String

These strings only contains one variable or property or array. When the goal is to convert a variable to a string, it is recommended to use the type casting (string) operator : it is then clearer to understand the conversion. It is also marginally faster, though very little.

<?php

$a = 0;
$b = "$a"; // This is a one-variable string

// Better way to write the above
$b = (string) $a;

// Alternatives :
$b2 = "$a[1]"; // This is a one-variable string
$b3 = "$a->b"; // This is a one-variable string
$c = "d";
$d = "D";
$b4 = "{$$c}";
$b5 = "{$a->foo()}";

?>

See also Strings and Type Juggling.

1.2.882.1. Connex PHP features

1.2.882.1.1. Suggestions

  • Drop the surrounding string, keep the variable (or property…)

  • Include in the string any concatenation that comes unconditionally after or before

  • Convert the variable to a string with the (type) operator

1.2.882.1.2. Specs

Short name

Type/OneVariableStrings

Rulesets

All, Analyze, CE, CI-checks, Changed Behavior

Exakat since

0.8.4

PHP Version

All

Severity

Minor

Time To Fix

Instant (5 mins)

Precision

Very high

Examples

Tikiwiki, NextCloud

Available in

Entreprise Edition, Community Edition, Exakat Cloud