Concatenation of two strings in PHP
Last Updated :
16 Sep, 2024
In this article, we will concatenate two strings in PHP. There are two string operators.
The first is the concatenation operator ('.'), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator ('.='), which appends the argument on the right side to the argument on the left side.
Examples:
Input : string1: Hello
string2 : World!
Output : HelloWorld!
Input : string1: geeksfor
string2: geeks
Output : geeksforgeeks
Example 1:
PHP
<?php
// First String
$a = 'Hello';
// Second String
$b = 'World!';
// Concatenation Of String
$c = $a.$b;
// print Concatenate String
echo " $c \n";
?>
Time complexity : O(n)
Auxiliary Space : O(n)
Example 2:
PHP
<?php
// First String
$fname = 'John';
// Second String
$lname = 'Carter!';
// Concatenation Of String
$c = $fname." ".$lname;
// print Concatenate String
echo " $c \n";
?>
Time complexity : O(n)
Auxiliary Space : O(n)
Example 3:
PHP
<?php
// First String
$a = 'Hello';
// now $a contains "HelloWorld!"
$a .= " World!";
// Print The String $a
echo " $a \n";
?>
Time complexity : O(n)
Auxiliary Space : O(n)
Example 4:
PHP
<?php
// First String
$a = 'Geeksfor';
// Second String
$b = "Geeks";
// now $c contains "GeeksforGeeks"
$c = "$a{$b}";
// Print The String $c
echo " $c \n";
?>
Time complexity : O(n)
Auxiliary Space : O(n)
PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.
Explore
Basics
Array
OOPs & Interfaces
MySQL Database
PHP Advance