Sometimes we need to add “two variables” or “one variable and one string”.
In this PHP tutorial we will see how to add two variables / string.
Example one:
Simply we can put all variables under double quotation mark.
<?php $str1 = 'welcome to '; $str2 = '9zap.com'; echo "$str1 $str2"; ?>
Example Two:
What happens if we put all variables under single quotation mark
<?php $str1 = 'welcome to '; $str2 = '9zap.com'; echo '$str1 $str2'; ?>
Example Three:
You can use the PHP concatenation operator which is a dot mark (.)
<?php $str1 = 'welcome to '; $str2 = '9zap.com'; echo $str1 . $str2; ?>
Example Four
Add some string / text and two variables.
<?php $str1 = 'welcome to '; $str2 = '9zap.com'; echo 'Hello world '.$str1 . $str2; ?>
Example Five
Another example to add two string and two variables and also insert a empty space.
<?php $name = 'Ravi'; $age = 28; echo 'Hello, may name is '.$name.' '.'and I am '.$age.' years old'; ?>