PHP provides special operators designed to work with strings. These operators allow you to join, append, and build dynamic text values efficiently inside your programs.
.) for string concatenation.=
// Concatenation operator
string1 . string2;
// Concatenation assignment operator
string1 .= string2;
// PHP example demonstrating string concatenation
<?php
$greeting = "Hello, ";
$name = "John!";
$message = $greeting . $name;
echo $message;
?>
The output of the above code will be:
Hello, John!
The . operator joins the values of $greeting and $name into a single string which is then displayed using echo.
Test the . operator live! Enter text below to see how PHP would join them.
// JavaScript simulation of PHP string concatenation
let firstName = "John";
let lastName = "Doe";
let fullName = firstName + " " + lastName;
console.log(fullName);
. operator for clear and readable string combinations.= when modifying existing stringsstring_operators.php.= operator to append a greeting message