Computer >> Computer tutorials >  >> Programming >> PHP

Concatenation of two strings in PHP program


To concatenate two strings in PHP, the code is as follows −

Example

<?php
   $a = 1.967;
   $b = 1.969;
   $res = $a." and ".$b;
   echo " $res \n";
   if((round($a, 2)) == (round($b, 2))) {
      echo "\nBoth the values are equal!";
   } else {
      echo "\nBoth the values aren\'t equal";
   }
?>

Output

This will produce the following output−

1.967 and 1.969
Both the values are equal!

Example

Let us now see another example −

<?php
   $val1 = 'Jack';
   $val2 = 'Sparrow';
   echo "First Name = $val1 \n";
   echo "Second Name = $val2 \n";
   $res = $val1.$val2;
   echo "Concatenation = $res \n";
?>

Output

This will produce the following output−

First Name = Jack
Second Name = Sparrow
Concatenation = JackSparrow