How to use the Concatenation Assignment (.=) Operator In PHP

The .= operator in PHP is the concatenation assignment operator. It is used to concatenate the right operand to the left operand and assign the result to the left operand.

Example: The below method illustrates how to add a Character to a String in PHP with the help of the Concatenation Assignment (.=) Operator.

PHP




<?php
$oldstr = "w3wiki";
$oldstr .= "!!";
echo $oldstr;
?>


Output:

w3wiki!!

How to add a Character to String in PHP ?

Given two strings, the task is to add a Character to a String in PHP. The character can be added to a string in many ways in this article we will be using eight methods including the Concatenate Method, the String Interpolation Method, the str_pad() Method, Concatenation Assignment (.=) Operator, String Conversion, The sprintf Function or printf Function, The strcat Function, and Using Array Join.

For Example, Consider the below example where there are two strings:

Input
string1 = "Welcome to" , 
string2 = "w3wiki"

Output
Welcome to w3wiki

Table of Content

  • Using Concatenate Method
  • Using String Interpolation Method
  • Using str_pad() Method
  • Using the Concatenation Assignment (.=) Operator
  • Using the String Conversion
  • Using the sprintf Function or printf Function
  • Using the strcat Function
  • Using Array Join

Similar Reads

Using Concatenate Method

The concatenation operator (.) of PHP offers to concatenate strings. Here we will be using the (.) sign to add a Character to the String. The example uses two individual strings to add and make a new string....

Using String Interpolation Method

...

Using str_pad() Method

The method is available in PHP 7.0 and later versions. The variables can be directly embedded in the resulting string without using the concatenation operator. With the help of the string interpolation method, we can add a Character to a String....

Using the Concatenation Assignment (.=) Operator

...

Using the String Conversion

The str-pad() method is used to add a character to either the beginning, the end, or both sides of the string. The string str_pad method takes four parameters including an old string, length, a new string that would be added pad_string, and pad type for defining side (start, end, or both)....

Using the sprintf Function or printf Function

...

Using the strcat Function

The .= operator in PHP is the concatenation assignment operator. It is used to concatenate the right operand to the left operand and assign the result to the left operand....

Using Array Join

...