All articles

Work with PHP values and data

Put a PHP Variable Next to Text Without Changing Its Name

Learn how to print a PHP variable beside a suffix, why single quotes print its name literally, and how braces or concatenation fix the boundary.

Editorial illustration for Insert PHP Variables into Strings with Braces or Concatenation

To put a PHP variable next to text, interpolate it in a double-quoted string. If letters must follow its value immediately, write {$name} to mark where the variable name ends: "{$name}son". If you prefer single-quoted text, place the variable between fragments joined with .. These choices matter when a suffix could be mistaken for part of the variable name, or when you expect a single-quoted greeting to contain a value rather than the characters $name.

See the boundary in one example

This example uses one variable throughout. Each echo includes a double-quoted "\n" so the expected results can be read on separate lines. The newline is supplied by the code, not added automatically by echo.

<?php
$name = 'Ada';

echo "Hello, $name!\n";
echo "{$name}son\n";
echo 'Hello, $name!' . "\n";
echo 'Hello, ' . $name . '!' . "\n";
echo $name . 'son' . "\n";

Expected text:

Hello, Ada!
Adason
Hello, $name!
Hello, Ada!
Adason

The first two lines use interpolation inside double quotes. The third keeps $name inside single quotes and therefore prints those characters literally. The last two join a variable value to quoted text instead. They demonstrate that choosing single-quoted fragments does not prevent you from including the value; it means the variable must sit outside those fragments.

Interpolate when the name is clear

In "Hello, $name!", the variable reference is inside a double-quoted string. PHP uses the value of $name, so the greeting contains Ada. The PHP manual’s echo example likewise puts a variable in a double-quoted string and shows its value in the output.

This short spelling works well for the greeting because the exclamation mark does not look like another letter in the variable name. The suffix son presents a different question. You want the value Ada followed immediately by son, not a reference whose name includes those letters. Write "{$name}son" to make the boundary explicit. PHP reads the variable inside the braces and leaves son as adjoining text, giving Adason. The braces serve as a delimiter here; they are not characters in that output.

A PHP manual user-contributed example contrasts a braced variable beside letters with an unbraced spelling that can be read as a different variable name. You do not need to try an undefined variable to see the useful rule: when letters touch the right side of an interpolated variable, mark its end with braces.

Keep single-quoted fragments, but join the value outside them

The third echo deliberately uses 'Hello, $name!'. Because $name is inside single quotes, that greeting remains literal text. Adding "\n" afterward puts it on its own line, but it does not change how the earlier single-quoted fragment is read. This distinction is easy to miss when two expressions appear in one echo statement.

For a greeting built from single-quoted fragments, use 'Hello, ' . $name . '!'. The variable is now between the fragments instead of inside either one. PHP’s string operators documentation defines . as the concatenation operator, which joins its left and right arguments. In the example, those joins produce Hello, Ada!.

The same approach handles the suffix: $name . 'son' produces Adason. Choose direct interpolation for a clear variable reference, braces when adjacent letters need a boundary, and . when you want to join quoted fragments with a variable outside them. Braces delimit a variable inside an interpolated string; they are not needed when the variable already stands separately between concatenated pieces.

← All articles