Wednesday, July 3, 2024
HomeLanguagesPhpConcatenation of two strings in PHP

Concatenation of 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: neveropenfor
        string2: neveropen
Output : neveropen

Code #1: 

PHP




<?php
 
// First String
$a = 'Hello';
 
// Second String
$b = 'World!';
 
// Concatenation Of String
$c = $a.$b;
 
// print Concatenate String
echo " $c \n";
?>


Output

 HelloWorld! 
 

Time complexity : O(n) 
Auxiliary Space : O(n)

  Code #2 : 

PHP




<?php
 
// First String
$fname = 'John';
 
// Second String
$lname = 'Carter!';
 
// Concatenation Of String
$c = $fname." ".$lname;
 
// print Concatenate String
echo " $c \n";
?>


Output

 John Carter! 
 

Time complexity : O(n) 
Auxiliary Space : O(n)

  Code #3 : 

PHP




<?php
 
// First String
$a = 'Hello';
 
// now $a contains "HelloWorld!"
$a .= " World!";
 
// Print The String $a
echo " $a \n";
?>


Output

 Hello World! 
 

Time complexity : O(n) 
Auxiliary Space : O(n)

  Code #4 :

PHP




<?php
 
// First String
$a = 'Geeksfor';
 
// Second String
$b = "Geeks";
 
// now $c contains "neveropen"
$c = "$a{$b}";
 
// Print The String $c
echo " $c \n";
?>


Output

neveropen
 

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.

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Nicole Veronica Rubhabha
Nicole Veronica Rubhabha
A highly competent and organized individual DotNet developer with a track record of architecting and developing web client-server applications. Recognized as a personable, dedicated performer who demonstrates innovation, communication, and teamwork to ensure quality and timely project completion. Expertise in C#, ASP.Net, MVC, LINQ, EF 6, Web Services, SQL Server, MySql, Web development,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments