
PHP | Combine two strings
There are several ways to concatenate two strings in PHP.
Use the concatenation operator [.] and [.=]
There are two string operators that are used for concatenation.
[.]: Concatenation operator | Used to combine two string values to create a new string.
[.=]: Concatenation assignment operator | Appends the argument on the right side to the argument on the left side.
Concatenation operator
<?php
$str1="Hello World! ";
$str2="Welcome to StudyZone4U.com";
echo $str1 . $str2;
// output
//This will produce following result −
//Hello World! Welcome to StudyZone4U.com
?>
Concatenation assignment operator
<?php
$str1 = "Hello World! ";
// Use og concatenation assignment operator
$str2 .= "Welcome to StudyZone4U.com";
echo $str2;
// Output:
// Hello World! Welcome to StudyZone4U.com
?>
Double quotes strings
PHP variables also work inside double-quotes. So if you place the PHP variable inside double quotes and just put a space between both the PHP variable will work the same
<?php
$str1 = "Hello World!";
$str2 = "Welcome to StudyZone4U.com";
// Use of Double quotes
// The curly braces have used here to make the things clear.
echo "{$str1} {$str2}";
// Output:
// Hello World! Welcome to StudyZone4U.com
?>
In the above example, you can directly use the PHP variable without curly braces. The curly braces have used in the above example to make things clear.
<?php
// without curly braces
echo "$str1 $str2";
// Output:
// Hello World! Welcome to StudyZone4U.com
?>
Use a cmmma "," with echo() statement
This used only when you what to print the thing directly on the webpage. The echo statement can take multiple variables as a list by using a comma to separate the variables.
So, let's see who its work
<?php
$str1 = "Hello World!";
$str2 = "Welcome to StudyZone4U.com";
echo $str1, ' ', $str2;
// Output:
// Hello World! Welcome to StudyZone4U.com
?>