SHARE

String and variable concatenation

In PHP concatenate means link together or join two or more variable ,join two or more strings or join variables with strings .

user can concatenate the strings and variables . In PHP “. ” dot sign is used for concatenation . By concatenate string and variable you can get the same result with shorter codes .

Syntax for concatenate two variables

$variable1.$variable2

Example

<?php
$var1 = "This is String one";
$var2 = "This is String Two";
echo $var1.$var2;
?>

 

Syntax for concatenate two Strings

‘string one’.’string two’

Example

<?php
echo   'This is String one'.'This is String Two';
?>

Syntax for concatenate variable with string

‘string one’.$variable

<?php
$var1 = "This is String one";
echo $var1.' This is String Two';
?>

Complex concatenation

‘string one’.$variable.’string two’

Example

<?php
$var2 = "This is String Two";
echo 'This is String one'.$var2.'This is String Three';
?>

Example variable output without Concatenation

<?php
$var1 = "This is String one";
$var2 = "This is String Two";
echo $var1;
echo $var2;
?>

Result

when you run this code the result is looks like as following image .

 

Variable Concatenation
Variable Concatenation

 

Example variable output with Concatenation

<?php
$var1 = "This is String one";
$var2 = "This is String Two";
echo $var1.$var2;
?>

Result

When you concatenate the variable or string the result is still same like upper image but the code is shorten . Result is “This is String oneThis is String Two ” .