This is a quick tutorial on concatenating strings in PHP. Find out the different methods using which you can concatenate multiple strings in PHP.
Table of Contents
PHP Concatenation of Strings
It’s really simple to concatenate Strings in PHP and there are several different ways using which you can concatenate two or more strings. I’ve mentioned below the three different approaches to concatenate two different strings in a simple PHP script.
Using Concatenation Operator .
This is the simplest way of concatenating any number of strings in PHP. You can concatenate any number of String Literals or String-type variables by simply using the concatenation operator .
between them. Consider the following PHP code.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php #PHP String Concatenation Method 1 #Using Concatenation Operator $firstName = "Gurmeet"; $lastName = "Singh"; #Concatenating firstName, space & lastName $full_name = $firstName." ".$lastName; echo $full_name; ?> |
In the above PHP Code Snippet, I’ve defined two string-type variables, the first one is $firstName
& the other one is $lastName
. Then we’re concatenating these two variables using the concatenation operator .
along with another space string between them. The concatenated string is then printed to the console using the echo statement.
Using Assignment-Concatenation Operator .=
You can make use of the Assignment-Concatenation Operator .=
to append one or more strings to any existing String variable or String literal. The following example illustrates the use case of this operator for concatenation of my First & Last name.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php #PHP String Concatenation Method 2 #Using Assignment-Concatenation Operator $firstName = "Gurmeet"; $lastName = "Singh"; $firstName .= " "; $firstName .= $lastName; echo $firstName; ?> |
Here, we’re simply concatenating new string literal space and new string variable $lastName
with the previous $firstName
. This method of string concatenation is quite helpful when you have to store very long strings into a single variable but you want to write the string content in the code line by line.
Using Double Quoted String Formation
In this method, we simply substitute the different variables in a double-quoted string literal so as to form a new string. Make sure this method only works with a new string literal formed using double quotes and not single quotes.
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php #PHP String Concatenation Method 3 #Using Double Quoted String Formation $firstName = "Gurmeet"; $lastName = "Singh"; $full_name = "$firstName $lastName"; echo $full_name; ?> |
In the above PHP Code, I’ve directly substituted the variables $firstName
and $lastName
in a new string literal created using double-quotes. This way the $full_name
variable now contains the concatenated string along with the space between the first and the last name. This method is often used when you want multiple string variables to be placed at different positions in a new string for formation with any more characters the way you want.
I hope you found this guide useful. If so, do share it with others who are willing to learn PHP and other programming languages. If you have any questions related to this article, feel free to ask us in the comments section.
And do not forget to subscribe to WTMatter!