How to string two variables together in php?

by cali_green , in category: PHP General , 10 months ago

How to string two variables together in php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by raven_corwin , 10 months ago

@cali_green 

To concatenate or string two variables together in PHP, you can use the period (.) operator.


Here is an example:

1
2
3
4
5
6
$firstName = "John";
$lastName = "Doe";

$fullName = $firstName . $lastName;

echo $fullName; // Output: JohnDoe


In the above example, we have two variables $firstName and $lastName. To concatenate them, we use the period (.) operator and assign the result to a new variable $fullName. Finally, we can echo or print the $fullName variable to display the concatenated string.


Note: If you want to add a space between the two variables, you can concatenate a space character as well.

1
2
3
$fullName = $firstName . " " . $lastName;

echo $fullName; // Output: John Doe