Advertisement

Google Ad Slot: content-top

PHP Concatenate Strings


String concatenation is used to join two or more strings together into a single string. PHP provides the dot operator (.) for this purpose.

Example
<?php
$firstName = "John";
$lastName = "Doe";
// Concatenate strings using the . operator
$fullName = $firstName . " " . $lastName;
echo $fullName; // Outputs: John Doe
?>
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
<?php
$x = "Hello";
$y = "World";
$z = "$x $y";
echo $z;
?>
Try it yourself