1.2.209. Concat Empty String

Using a concatenation to make a value a string should be replaced with a type cast.

Type cast to a string is done with (string) operator. There is also the function strval(), although it is less recommended.

<?php

$a = 3;

// explicite way to cast a value
$b = (string) $a; // $b is a string with the content 3

// Wrong way to cast a value
$c = $a . ''; // $c is a string with the content 3
$c = '' . $a; // $c is a string with the content 3
$a .= '';     // $a is a string with the content 3

// Wrong way to cast a value
$c = $a . '' . $b; // This is not reported. The empty string is useless, but not meant to type cast

?>

See also Type Casting and PHP Type Casting.

1.2.209.1. Suggestions

  • Avoid concatenating with empty strings

  • Use (string) operator to cast to string

  • Remove any concatenated empty string

1.2.209.2. Specs

Short name

Structures/ConcatEmpty

Rulesets

All, Analyze

Exakat since

1.8.0

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Very high

Features

string

Available in

Entreprise Edition, Exakat Cloud