PHP String Operators

The operators are tokens of a programming language that allow some operation on variables. Strings in any programming language can be combined or assigned to a variable. PHP String operators are (.) dot operator and (.=) concatenation assignment operator .

Dot Operator (.)

The dot operator can be placed between two strings to combine them together to form one string. The final string made has the same sequence of words as the sequence in which the strings are joined together.

Consider the following example

<?php
$my_hello='Hello ';
$my_world='World ';
$my_welcome='Welcome ';
$my_php= 'Come and learn ';
$my_site= "at CSVeda.com ";
echo $my_hello.$my_world.'<br>';
echo $my_welcome.$my_world.'<br>';
echo $my_welcome.$my_site.'<br>';
echo $my_hello.$my_world.'!! '.$my_welcome.$my_php.$my_site.'<br>';
?>

The Output is

Hello World 
Welcome World 
Welcome at CSveda.com 
Hello World !! Welcome Come and learn at CSVeda.com

You can join strings stored in variables or concatenate variables and strings defined with single or double quotes within a single echo statement or assignment statement

Caution: if you are joining different strings then you must make sure that using dot string operator will not create space between the strings. You have to include blank space at beginning and/or at the end of the strings to avoid joining of words.

Concatenation Assignment Operator (.=)

Concatenation Assignment operator behaves like an assignment operator that combines the string on the right hand side with the variable value on the left hand side and updates the variable’s value with concatenated string

consider this example

<?php
$my_msg="";
$my_hello='Hello ';
$my_world='World ';
$my_welcome='Welcome ';
$my_php= 'Come and learn ';
$my_site= "at CSVeda.com ";
$my_msg.=$my_hello;
echo $my_msg.'<br>';
$my_msg.=$my_world;
echo $my_msg.'<br>';
$my_msg.=$my_welcome;
echo $my_msg.'<br>';
$my_msg.=$my_site.'<br>';
echo $my_msg.'<br>';
?>

The output is

Hello 
Hello World 
Hello World Welcome 
Hello World Welcome at CSVeda.com

You can see that using the concatenation assignment operator the  $my_msg  variable is updated with its current string appended with the string on right hand side of the operator.