How can I combine two strings together in PHP?

How can I combine two strings together in PHP?

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

Stefan Bogdanescu

Founder & Senior Architect

String Concatenation in PHP: Beyond Simple Juxtaposition

You have two variables, $data1 and $data2, and you need them to become a single string. The example provided shows the color is meeting red to form the color is red. While the goal seems straightforward, the method you choose depends heavily on your context: are you building a simple debug message or constructing an SQL query? PHP offers several operators for this task, each with distinct implications for readability, performance, and security.

The Period Operator

The most idiomatic approach in PHP for joining strings is the period operator (.). It is explicit, readable, and universally understood by other developers reading your codebase.

$data1 = "the color is";
$data2 = "red";
$result = $data1 . $data2;

echo $result; // Outputs: the color is red

This syntax has zero overhead in modern PHP versions and scales well when chaining multiple strings together without introducing ambiguity. If you need to insert a space between two distinct variables, simply add it as part of one of the operands or include it in the concatenation chain. For instance, $result = $data1 . ' ' . $data2; ensures the output includes that crucial whitespace. The period is the default choice for general-purpose string assembly because it avoids the parsing quirks associated with other operators.

The Plus Operator and Type Coercion

You might be tempted to use the plus sign (+) for addition, assuming it acts like a concatenation operator in JavaScript or Python. This will not work as intended if you are strictly dealing with strings. In PHP, the + operator attempts arithmetic addition first. If both operands are purely numeric strings (e.g., "10" and "20"), PHP converts them to integers and sums them, resulting in a number (30). If either operand contains non-numeric characters, PHP performs type coercion to cast them to zero before adding, often yielding unexpected results like 0 or relying on implicit conversion rules that can obscure bugs.

$data1 = "the color is"; // Non-numeric string
$data2 = "red";          // Non-numeric string
$result = $data1 + $data2; 

echo $result; // Outputs: 0 (due to type coercion)

Relying on + for text manipulation is a common pitfall that leads to silent data loss. Unless you are certain both variables contain numeric strings and intend an arithmetic sum, avoid this operator for string combination. Stick to the period or dedicated functions to maintain predictable behavior.

The Ampersand Operator

The ampersand (&) can also concatenate strings, but it comes with a specific side effect: reference assignment. When you write $result = &$data1 . $data2;, PHP creates a reference between the result and the first operand. While this saves memory in highly specific scenarios involving large string buffers, introducing references into standard string concatenation logic complicates debugging and refactoring. Most developers will never need to use & for simple text joining, so it adds unnecessary cognitive load without providing tangible benefits in standard application flows.

Using the Concatenation Operator with Arrays

When your data sources are arrays rather than scalar strings, you might consider looping through them or using implode. The period operator does not accept arrays directly. If you have an array of parts like ['the', 'color', 'is'], you must use implode(' ', $array) to join them with a specific delimiter. Attempting $result = implode($data1, $data2); will fail if the arguments are not formatted as expected by implode, which expects an array of strings and a separator string.

Security Considerations in String Assembly

The most critical aspect of combining strings is not performance, but security. If you are assembling SQL queries or shell commands from user input stored in $data1 and $data2, simple concatenation becomes a vector for injection attacks. Even if the source material suggests a benign "the color is red" example, real-world applications often deal with untrusted input.

// Vulnerable to SQL Injection if $data1 comes from a user
$sql = "SELECT * FROM products WHERE name = '$data1'";

In such cases, you should avoid string concatenation entirely in favor of prepared statements using PDO or the Query Builder provided by frameworks like Laravel. If you are working within a Laravel context, utilizing str facade methods or Eloquent relationships provides safer abstractions for string manipulation and data binding. For more information on secure development practices and utility tools, resources such as https://laravelcompany.com offer guidance on building robust applications that handle input safely.

By prioritizing the period operator for standard text joining and avoiding arithmetic operators or references unless strictly necessary, you ensure your code remains readable, performant, and secure against common implementation errors.

Our network