Skip to main content

How to Join Strings in PHP with the Dot Operator

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

PHP joins strings with a dot, not a plus: $first . ' ' . $last. To add to the end of a string you already have, use .=. Numbers can join in too; PHP turns them into text.

Syntax​

Syntax
$<text> = <string> . <string>;
$<text> .= <string>;
PartWhat it does
<string> . <string>A new string with the right side joined to the left. Numbers are turned into text first.
$<text> .= <string>Joins the string onto the end of $<text> and stores the result in $<text>.

The PHP manual on string operators: "There are two string operators. The first is the concatenation operator ('.'), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator ('.='), which appends the argument on the right side to the argument on the left side."

The Example​

The example joins a first and a last name with a space, joins a sentence with a year in it, and builds a greeting with .=.

Prerequisites​

  • PHP for the command line. This guide uses PHP 8.6.0.

Step 1: Write the Code​

Create a file named main.php:

main.php, which joins Ada, a space and Lovelace with dots, echoes it, echoes it joined with was born in and the year 1815, and adds a comma and Ada to Hello with dot equals.

main.php
<?php

$first = 'Ada';
$last = 'Lovelace';
$year = 1815;

$full = $first . ' ' . $last;
echo $full . "\n";
echo $full . ' was born in ' . $year . "\n";

$greeting = 'Hello';
$greeting .= ', ' . $first;
echo $greeting . "\n";

Step 2: Run It​

Terminal
php main.php
Output
Ada Lovelace
Ada Lovelace was born in 1815
Hello, Ada

A zsh terminal where php main.php prints Ada Lovelace, Ada Lovelace was born in 1815, and Hello, Ada.

The command is the same in zsh, bash and PowerShell.

What Happened​

  • $first . ' ' . $last joined three strings.
  • $year is a number, 1815, and the dot turned it into text before joining.
  • $greeting .= ', ' . $first joined , Ada onto Hello and stored the result back in $greeting.

Common Mistakes​

MistakeWhat happensDo this instead
Joining with +, as in 'Ada' + 'Lovelace'TypeError: Unsupported operand types: string + stringUse .
Mixing . and + without brackets"a" . 1 + 2 gives a3: + runs firstPut arithmetic in brackets: "a" . (1 + 2)
Putting a function call inside double quotes"{strtoupper($name)}" printed {strtoupper(Ada)}: the function isn't calledJoin it: 'Hi ' . strtoupper($name)

Troubleshooting​

SymptomLikely causeFix
Unsupported operand types: string + string+ was used to join stringsUse .
A number appears added instead of joined+ ran before . in a mixed expressionAdd brackets

FAQ​

When should I use double-quoted interpolation instead?​

When the text has variables in it: "Hello, $name" reads more easily than 'Hello, ' . $name. Use the dot when you join the results of function calls or expressions.

How do I join an array of strings?​

With implode(', ', $items), which puts the separator between the items.

Conclusion​

. joins strings in PHP and .= adds to the end of one. Numbers join as text, but + on strings is a TypeError, and in a mixed expression + runs before ., so use brackets.

Additional Resources​