Zum Inhalt springen

String concatenation in loop (ARCH7070)

Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.

Key php.performance.string-concat-in-loop · Severity info · Category performance

Appending to a string with .= on every iteration can reallocate the buffer each time, degrading to O(n^2) as input grows. Collect the pieces and join them once at the end.

<?php
$out = '';
foreach ($rows as $row) {
$out .= $row . "\n";
}
echo $out;
<?php
$parts = [];
foreach ($rows as $row) {
$parts[] = $row;
}
echo implode("\n", $parts);

Collect pieces into an array and join them once with implode() after the loop finishes.