String concatenation in loop (ARCH7070)
هذا المحتوى غير متوفر بلغتك بعد.
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.
Incorrect
Section titled “Incorrect”<?php$out = '';foreach ($rows as $row) { $out .= $row . "\n";}echo $out;Correct
Section titled “Correct”<?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.