PHP: How To Concatenate Strings

In PHP, the operator to concatenate or combine strings together is: . (dot)

Here are some examples of usage when concatenating strings:

<?php

$string1 = ‘tech’;
$string2 = ’saints’;
$string3 = ‘.com’;

$full = $string1.$string2.$string3;

echo $full;

?>

Result: techsaints.com

<?php

$string = ‘techsai’;
$string .= ‘nts’;
$string .= ‘.com’;

echo $str;

?>

Result: techsaints.com

<?php

$num = 123;

$string = $num.’ is a number’;

echo $string;

?>

Result: 123 is a number

When concatenating a number and a string, the string is automatically cast as a string.

Leave a Reply