All lessons

Working with Text

7 min · PHP

Join strings, measure them, and embed variables.

PHP · Working with Text0/5 cleared

Enemy 1 of 5

Strings and concatenation

This code joins two pieces of text into one with the . operator. The comma and space are already inside the first string. Press Run to see the combined greeting.

Why this works

A string is just a run of text, and PHP lets you write one with either double quotes ("...") or single quotes ('...'). The two look similar but behave differently, which trips up almost every beginner at least once. More on that below.

To combine two strings into one, PHP uses the . operator, called concatenation. It doesn't add a space for you automatically, so "Hello" . "World" produces "HelloWorld" with no gap. If you want a space, you have to include it yourself, either as its own string ("Hello" . " " . "World") or baked into one of the pieces, as the example below does.

You
Packet Wolf
Loading editor…
Sign up to fightThis one runs on our servers, so it needs a free account.