Home » PHP string Concatenation

PHP string Concatenation

by Online Tutorials Library

PHP string Concatenation

In this article, we will create a string concatenation in PHP. In this, we will learn the basics of PHP string. After that we will learn this concept with the help of various examples.

What do you mean by string in PHP?

A string is a collection of characters. It offers various types of string operators and these operators have different functionalities such as string concatenation, compare values and to perform Boolean operations.

PHP string Concatenation

PHP string concatenation operator is used to concatenate the strings in PHP. There are two types of string operators provided by PHP.

1. Concatenation Operator (“.”):

In PHP, this operator is used to combine the two string values and returns it as a new string.

Let’s take the various examples of how to use the Concatenation Operator (“.”) to concatenate the strings in PHP.

Example 1:

Explanation:

In the above example, we have concatenated the two strings in the third string. In this we have taken two variables $a and $b as a string type and $c variable is also string type in which the concatenate string is stored.

For this purpose we have used the Concatenation operator (.) to concatenate the strings.

Output:

Below figure demonstrates the output of this example:

PHP string Concatenation

Example 2:

Explanation:

In the above example, we have concatenated the two strings in the third string. In this we have taken two variables $fname and $lname as a string type and $c variable is also string type in which the concatenate string is stored.

For this purpose we have used the Concatenation operator (.) to concatenate the strings.

Output:

Below figure demonstrates the output of this example:

PHP string Concatenation

2. Concatenating Assignment operator (“.=”):

This operation appends the argument on the right side to the argument on the left side.

Let’s take the various examples of how to use the Concatenation Assignment Operator (“.=”) to concatenate the strings in PHP.

Example 1:

Explanation:

In the above example, we have concatenated the two strings in one string variable. In this we have taken a variable $string1 in which the concatenate string is stored. For this purpose we have used the Concatenation Assignment operator (“.=”) to concatenate the strings.

Output:

Below figure demonstrates the output of this example:

PHP string Concatenation

Example 2:

Explanation:

In the above example, we have concatenated the variable in the array string. In this we have taken a variable $a and & b string array in which the concatenate string is stored. For this purpose we have used the Concatenation Assignment operator (“.=”) to concatenate the strings.

Output:

Below figure demonstrates the output of this example:

PHP string Concatenation


You may also like