String Concatenation in PHP
String concatenation in PHP refers to joining two or more strings into one. It is a fundamental operation in PHP, which allows you to combine multiple strings into one string.
For example, suppose you’re filling out a registration form where you enter your first name and last name. Let’s say:
- First Name: John
- Last Name: Smith
After submitting the form, the application needs to display your full name on your profile page. To do this, it combines your first name and last name with a space in between like this:
- Full Name: John Smith
This process of joining the first name and last name to form a complete full name is known as string concatenation in PHP or any other programming languages. We can write the code for it like this:
<?php
$firstName = "John";
$lastName = "Smith";
$fullName = $firstName . " " . $lastName;
echo $fullName;
?>
Output: John Smith
In this example, we have concatenated $firstName
, a space (" "
), and $lastName
. using dot (.) operator. Then, we have displayed full name using echo statement.
Ways to Concatenate Strings in PHP
As of the latest PHP versions, there are mainly two ways to concatenate strings in PHP. The most common ways to concatenate strings are as follows:
- Using the Dot Operator (.)
- Using the Concatenation Assignment Operator (.=)
Dot Operator (.) for String Concatenation in PHP
The dot operator (.) is the standard and simplest way to concatenate multiple strings in PHP. The general syntax to concatenate more than one string in PHP is as:
$result = $string1 . $string2 . $string3 ... ;
In the above syntax, the . operator joins two or more strings together. Let’s take some examples on this method.
Example 1:
<?php
$firstName = "Saanvi";
// Concatenation using the dot operator.
$greeting = "Hello" . " " . $firstName . "!";
// Creating a chain of multiple string concatenations.
$message = "I" . " am" . " learning" . " PHP" . ".";
echo $greeting . "\n";
echo $message;
?>
Output: Hello Saanvi! I am learning PHP.
Example 2:
<?php
$name = "Jack";
$age = 25;
// Concatenating variable and string.
echo "Name: " . $name . "\n";
// Concatenating string with number.
echo "Age: " . $age;
?>
Output: Name: Jack Age: 25
In this example, PHP automatically converts numbers to strings during concatenation.
String Concatenation using Assignment Operator (.=)
Another simple way to concatenate multiple strings in PHP is using the concatenation assignment operator (.=). This operator appends a string to the end of another string. It modifies the original string by adding more string content to it. This method is useful when you want to concatenate string in multiline.
Example 3:
<?php
$firstName = "Saanvi";
// String concatenation across multiple lines.
$message = "Welcome, ";
$message .= $firstName;
$message .= "!";
echo $message;
?>
Output: Welcome, Saanvi!
String concatenation is one of the most common operations in PHP or many other programming languages. We frequently use string concatenation in applications to combine user inputs like first and last names in forms. I hope that you will have understood the basic ways to concatenate multiple strings in PHP and practiced all examples.