Can you append strings to variables in PHP?

Can you append strings to variables in PHP?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

String Accumulation in PHP: Why += Fails and What to Use Instead

You might expect that using the compound assignment operator += works universally for building up data structures, just as it does in JavaScript or when handling numeric values in PHP. However, attempting to concatenate strings this way produces unexpected results. If you run code where $selectBox starts as a string and you apply $selectBox += 'more text', the output is often empty or incorrect because the operator isn't doing what your intuition suggests.

When you execute $selectBox += '<option value="' . $i . '">' . $i . '</option>' starting with a string, PHP internally converts that expression into $selectBox = (string)($selectBox + 'more text'). The addition operator + is strictly reserved for arithmetic operations in PHP. When applied to two strings, it attempts to convert both operands to integers and performs integer addition. If the conversion fails—because the strings don't represent valid numbers—the result becomes 0. Consequently, $selectBox gets assigned the integer 0, which then casts back to an empty string when echoed. This explains why your loop outputs nothing; you are effectively resetting the variable to zero on every iteration.

This behavior highlights a fundamental difference in how different languages handle operators. In JavaScript, the + operator is overloaded to perform string concatenation if at least one operand is a string. PHP does not share this flexibility for the binary addition operator. To reliably append content to a variable containing text, you must use the dot (.) operator, which is explicitly designed for string concatenation.

The corrected approach replaces the compound assignment with explicit concatenation:

$selectBox = '<select name="number">';
for ($i = 1; $i <= 100; $i++) {
    $selectBox .= '<option value="' . $i . '">' . $i . '</option>';
}
$selectBox .= '</select>';

echo $selectBox;

By switching to $selectBox .= '...', you instruct PHP to treat the operation strictly as a string merge. The dot operator preserves the content of the left-hand side and appends the right-hand side without attempting any numeric conversion. This pattern is the standard for building HTML strings, logs, or any textual output in PHP scripts.

While some developers might reach for functions like strcat (a legacy C-style function) or assume that concatenation chains are inefficient, modern PHP engines handle simple dot operations efficiently enough for most use cases. If you are constructing very large strings or generating thousands of lines of HTML in a loop, it is worth considering alternatives to avoid excessive memory allocation and string copying. The str_contains check or building an array first before using implode() can be more performant in high-volume scenarios.

For instance, collecting options in an array and joining them at the end eliminates repeated string allocations:

$options = [];
for ($i = 1; $i <= 100; $i++) {
    $options[] = '<option value="' . $i . '">' . $i . '</option>';
}

$selectBox = '<select name="number">' . implode('', $options) . '</select>';

This technique separates the logic of data generation from string manipulation, often leading to cleaner code and better performance. If you are working within the Laravel ecosystem, you might find similar patterns useful when building dynamic views or generating JSON responses, although the core language behavior regarding += remains consistent across versions. For deeper insights into optimizing string handling in larger applications, resources like https://laravelcompany.com can provide context on best practices for scaling PHP applications.

Ultimately, avoiding += with strings is a simple rule that prevents subtle bugs where variables silently degrade to zero. By sticking to the dot operator or array-based construction, you ensure your string accumulation logic behaves predictably and aligns with how the language was intended to be used.

Our network