PHP - Concatenate Strings


String Concatenation

To concatenate, or combine, two strings you can use the . operator:

Example

$x = "Hello";
$y = "World";
$z = $x . $y;
echo $z;
Try it Yourself »

The result of the example above is HelloWorld, without a space between the two words.

You can add a space character like this:

Example

$x = "Hello";
$y = "World";
$z = $x . " " . $y;
echo $z;
Try it Yourself »

An easier and better way is by using the power of double quotes.

By surrounding the two variables in double quotes with a white space between them, the white space will also be present in the result:

Example

$x = "Hello";
$y = "World";
$z = "$x $y";
echo $z;
Try it Yourself »

Complete PHP String Reference

For a complete reference of all string functions, go to our complete PHP String Reference.


Copyright 1999-2023 by Refsnes Data. All Rights Reserved.