In this article, we shall focus on how to combine strings in PHP using different methods that are provided in the PHP language. You may have strings that are collected s different entities or inputs and you want to either store them as one or you may want to display them as one combined string. For example, in form input, we may have the first name and last name collected as two separate fields but while displaying the data we may want to display the name as first name and last name combined, this is made possible in PHP where you can combine strings to form one variable or result.
Remember, you can combine not only two strings but also an unlimited number of strings as long as you follow the structure.
First, when you want to combine strings in PHP, you will need to familiarize yourself with the concatenation operator represented by the (.) sign. This operator is the one used in PHP to add strings together. We shall highlight the two methods by which you can use the concatenation operator to combine strings.
The first way involves adding the concatenation operator between the strings you want to combine
Example 1
The strings to combine are 'John' and 'Doe'
<?php
echo 'John'. 'Doe';
?>
The result of the above will be
<?php
JohnDoe
?>
Note that the result above has made the two strings to be one continuous string. To avoid this such that the strings can be separated but read still as one string, we add white space between the two strings as shown below
<?php
echo 'John'. ' '. 'Doe';
?>
The result will now be updated to
John Doe
Example 2
We can combine strings that are stored in variables.
<?php
$firstname='John';
$lastname='Doe';
echo $firstname. ' '. $lastname;
?>
The result for the above will be same as the one above
John Doe
Example 3
To combine three strings, we will need to follow the same process
<?php
$firstname='John';
$lastname='Doe';
$country='USA';
echo $firstname. ' '. $lastname. ' '. $country;
?>
The result for the above code snippet will be
John Doe USA
The second way of how to combine strings in PHP is by assigning the strings to one variable. This is achieved by adding the concatenation operator as a prefix in the equals sign (.=). Adding the concatenation operator in front of the equals sign means that you are adding the current string to what is already present in that string.
Example 4
<?php
$name='John';
$name.='Doe';
echo $name;
?>
The result for the above code snippet will be as below
JohnDoe
Please note that we have used just one variable name called $name which had value John at the beginning, to add Doe to the first string, we have defined the same variable again where we have added the concatenation sign before the equals sign which has resulted to the above result.
To add a string between the strings while adding the above method of combining strings, we can either define a variable and assign it a white space or add a white space before the next variable.
Defining a variable with white space as a value
Example 5
<?php
$name='John';
$name.=' ';
$name.='Doe';
echo $name;
?>
The result will be
John Doe
To add a white space before the next variable
Example 6
<?php
$name='John';
$name.=' Doe';
echo $name;
?>
The result will be
John Doe
In the above discussions, we have seen the different kinds of ways of how to combine strings in PHP. The reason why PHP uses the concatenation operator is to ensure we achieve uniformity while working with strings either defined in variables or not.