Concatenation In Echo

ID

php.avoid_concat_echo

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Efficiency

Language

Php

Tags

efficiency, string-handling

Description

Reports an echo statement that joins its parts with the string concatenation operator (.) instead of passing them as comma-separated arguments. Pure literal-on-literal concatenation, which the compiler folds, is not flagged.

Rationale

echo accepts a comma-separated list of expressions and writes each one to the output buffer in turn. Writing echo $a . $b; instead first allocates and fills a temporary string holding the joined result, then prints that — extra work and memory that grows with the number of concatenated operands. echo $a, $b; avoids the intermediate string entirely. The comma form is also easier to read once several parts are involved.

<?php
echo $name . ': ' . $value;   // FLAW — builds a temporary joined string
echo $name, ': ', $value;     // OK — each part written directly to output
echo 'a' . 'b';               // OK — pure literal concatenation, folded at compile time

Remediation

Replace the concatenation operators between echo operands with commas. The output is identical, with no temporary string allocation.