How do I concatenate strings in PHP?

To concatenate strings in PHP, you can use the concatenation operator (' . ') and the concatenate assignment operator (' .= '). The concatenation operator (' . ') returns the concatenation result of the right and left arguments. When concatenating, each subsequent line is added to the end of the previous one. A value of any type that is concatenated with a string will be implicitly converted to a string and then concatenated. The assignment operator (' .= ') adds the argument on the right side to the argument on the left side. This is a shorter way to concatenate the arguments and assign the result to the same variable. In this PHP String Concatenation example, we use the concatenation operator (' . ') and return the concatenating arguments. Click Execute to run the PHP Concatenation String Example online and see the result.
How do I concatenate strings in PHP? Execute
<?php
$a = 'PHP';
$b = 'string';
$c = 'concatenation';
$d = 'example';

echo $a." ".$b." ".$c." ".$d;  
?>
Updated: Viewed: 832 times

How to concatenate strings using concatenate operator (' . ') in PHP?

To concatenate strings in PHP, you can use the concatenation operator (' . '), which will concatenate the strings and assign the result to a variable.

PHP Concatenation Operator (' . ') Example
<?php
$a = 'PHP';
$b = 'string';
$c = 'concatenation';
$d = 'example';

echo $a." ".$b." ".$c." ".$d; 
?>

#output: PHP string concatenation example

How to concatenate strings using the assignment operator (' .= ') in PHP?

To concatenate strings, use the concatenation assignment operator (' .= '), which adds the argument on the right side to the argument on the left side.

PHP Concatenating Assignment Operator (' .= ') Example
<?php
$full_name = 'John ';

$full_name .= 'Carter';

echo $full_name; 
?>

#output: John Carter

See also