PHP String Operators

Rumman Ansari   Software Engineer   2020-02-06   5451 Share
☰ Table of Contents

Table of Content:


PHP has two operators that are specially designed for strings.

Operator Name Example Result
. Concatenation $txt1 . $txt2 Concatenation of $txt1 and $txt2
.= Concatenation assignment $txt1 .= $txt2 Appends $txt2 to $txt1

Example: . Concatenation

Code:


<!DOCTYPE html>
<html>
<body>

<?php
$txt1 = "Happy";
$txt2 = " Coding!";
echo $txt1 . $txt2;
?>  

</body>
</html>


Output:

The above code will produce the following result-


Happy Coding!

Example: .= Concatenation assignment

Code:


<!DOCTYPE html>
<html>
<body>

<?php
$txt1 = "Happy";
$txt2 = " coding!!";
$txt1 .= $txt2;
echo $txt1;
?>  

</body>
</html>


Output:

The above code will produce the following result-


Happy coding!!