Skip to content
PHP Logo

What is the correct way to concatenate and format two variables in PHP?

When concatenating and formatting two variables in PHP, you can use the dot (.) operator to concatenate the variables and string literals. To format the concatenated variables, you can use double quotes ("") and include the variables inside curly braces ({}) preceded by a dollar sign ($). This will interpolate the variables within the string.

php
// Concatenate and format two variables in PHP
$variable1 = "Hello";
$variable2 = "World";

$formattedString = "{$variable1}, {$variable2}!";
echo $formattedString;