Table of Contents
PHP String Operators: Manipulating Strings in PHP
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!!