How will you concatenate two strings in PHP?
To join (concatenate) two strings in PHP, use the dot operator (.). To add text at the end of an existing variable, use .=.
<?php
$first = "Hello";
$second = "World";
echo $first . " " . $second . "<br>";
$text = "Hello";
$text .= " PHP";
echo $text . "<br>";
echo "$first $second";
?>
Output:
Tutorials Class - Output Window
Hello World
Hello PHP
Hello World
The last line puts the variables inside double quotes, which also works. Remember that the plus sign (+) does not join strings in PHP like in JavaScript, it tries to add numbers. Read more in PHP String Handling.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |