PHP String Concatenation



String concat is a system for adding two or more words with each other.  This is most of the time used in several conditions like inside function or to store the value of two or more variables in a single variable. For better clearance we can give an example. Let's see a string variable given value


<?php 
   
    $data1 = 'Parrot is a cute bird';
?>

We have declared a variable $data1 that stores a string "Paroot is a cute bird". This is a single string. We now want to add a string "It has a big beak also" with $data1 so that we get a single string "Paroot is a cute bird. It has a big beak also". For doing this we need to use concate operator(.)   So let's do this in the following code


<?php

    $data1 = 'Parrot is a cute bird';
    $data2 = "It has a big beak also";
    $finaldata = $data1 . " . " . $data2;
    echo $finaldata;
?>

Output:


Parrot is a cute bird . It has big beak also

  The output of the data after concatenation will be "Parrot is a cute bird . It has big beak also" that is stored in variable $finaldata. We can also use .= sign to concat like below.


<?php

    $data1 = 'Parrot is a cute bird';
    $data2 = "It has a big beak also";
    $data1 .= $data2;
    echo $data1;
    
?>

This process will also give same data as output.


Parrot is a cute bird . It has big beak also