PHP String Operators: Manipulating Strings in PHP

Rumman Ansari   Software Engineer   2024-07-09 10:10:36   5443  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

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!!