String concatenation means combining two or more strings into one. In PHP, you can achieve this with the .
operator.
Example 1: Basic Concatenation
You can combine two strings like this:
$brand = "Yamaha"; $model = "R1"; $full_name = $brand . $model; echo $full_name;
The output will be YamahaR1
, without any space between the words.
Example 2: Adding a Space
To add a space between the concatenated strings, you can include a space character:
$brand = "Yamaha"; $model = "R1"; $full_name = $brand . " " . $model; echo $full_name;
The output will now be Yamaha R1
.
Example 3: Using Double Quotes
A more straightforward way to concatenate strings with spaces is by using double quotes:
$brand = "Yamaha"; $model = "R1"; $full_name = "$brand $model"; echo $full_name;
By enclosing both variables in double quotes and placing a space between them, the output will also be Yamaha R1
.