In PHP it is common to join variables to text and/or HTML tags to produce a complete element: this is referred to as concatenation. The concatenation operator is the period (.) character.

You can concatenate the value of two variables to create a value for a third:

$firstName = "Dudley";
$lastName = "Storey";
$fullName = $firstName.$lastName;

In the example above, $fullName contains the value “DudleyStorey”. If you want a space in the value of $fullName, you can concatenate the variables together with a physical space:

$fullName = $firstName." ".$lastName;

Naturally you can also echo concatenated strings with variables:

echo "Your name is ".$firstName." ".$lastName;

Note that joining a number with a string creates a string:

$var1 = 23;
$var2 = "skidoo";
$var3 = $var1.$var2;

The value of $var3 is “23skidoo”.

It is common to “build up” the value of a single variable through concatenation, especially when the individual pieces are fairly long; for example, when composing an email to be sent via PHP.

$test = "This is a long ";
$test .= "string of text pieces ";
$test .= "all joined together.";

(Note the use of spaces).

At the end of this process the value of $test will be “This is a long string of text pieces all joined together.”

Enjoy this piece? I invite you to follow me at twitter.com/dudleystorey to learn more.