In this PHP tutorial, you shall learn how to concatenate two or more strings using String Concatenation Operator, with example programs.
PHP – Concatenate Strings
To concatenate strings in PHP, use string concatenation operator .
. String Concatenation operator takes two strings as operands and returns a string that is concatenation of the two operand strings.
The syntax to use string concatenation operator is
$result = $string_1 . $string_2
You can also concatenate more than two strings, just like an arithmetic expression where you add more than two integers. The syntax to concatenate more than two strings is
$result = $string_1 . $string_2 . $string_3 . $string_4
Examples
1. Concatenate two strings
In this example, we will take two strings in two variables, and concatenate them using concatenation operator. We shall store the returned string in a variable and echo it.
PHP Program
<?php
$string_1 = "Hello ";
$string_2 = "World!";
$result = $string_1 . $string_2;
echo $result;
?>
Output
2. Concatenate more than two strings
In this example, we will take three strings, and concatenate them using concatenation operator.
PHP Program
<?php
$string_1 = "Hello ";
$string_2 = "World!";
$string_3 = " Welcome to PHP Tutorial by TutorialKart.";
$result = $string_1 . $string_2 . $string_3;
echo $result;
?>
Output
Conclusion
In this PHP Tutorial, we learned how to concatenate two or more strings using string concatenation operator, with the help of example programs.