How to combine two strings in PHP
Answer: Use the PHP Concatenation Operator
You can use the PHP concatenation operator (
.) to combine or join two strings together in PHP. This operator is specifically designed for strings.Example
Run this code »$str1 = 'Hello';
$str2 = 'World!';
$new_str = $str1 . ' ' . $str2;
// variable . string . another varableecho $new_str; // Outputs: Hello World!
Code #1:
<?php// First String$a = 'Hello';// Second String$b = 'World!';// Concatenation Of String$c = $a.$b;// print Concatenate Stringecho " $c \n";?> |
Output :
HelloWorld!
Code #2 :
<?php// First String$fname = 'John';// Second String$lname = 'Carter!';// Concatenation Of String$c = $fname." ".$lname;// print Concatenate Stringecho " $c \n";?> |
Output :
John Carter!
Code #3 :
<?php// First String$a = 'Hello';// now $a contains "HelloWorld!"$a. = "World!";// Print The String $aecho " $a \n";?> |
Output :
HelloWorld!