Concatenate Strings in PHP (Tutorial)

Concatenate Strings In PHP

This is a quick tutorial on concatenating strings in PHP. Find out the different methods using which you can concatenate multiple strings in PHP.

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.

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.

PHP String Concatenation Using Concatenation Operator Example

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.

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.

PHP Concatenation Of Strings Using Assignment Concatenation Operator

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.

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.

PHP String Concatenation Using Double Quoted String Formation Example

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!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *