Working with Text
7 min · PHP
Join strings, measure them, and embed variables.
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.
Rena
Your AI tutor. She walks you through each step.
Asking for help is part of coding. Rena reads your code and gives you a hint. She is free with an account: 20 hints a day.