In this tutorial, you shall learn how to specify or include a variable in a string in PHP, with the help of example programs.

PHP – Variable in a string

To specify or include a variable in a string in PHP, wrap the variable in curly braces. Also, please make sure to use double quotes to write the string literal.

Syntax

The syntax to include the variable $name in a string is

"hello {$name}"

The expression interpolates the variable $name directly in the string.

ADVERTISEMENT

Example

In the following program, we take a value in variable $name, include it in the string, and echo the string to output.

PHP Program

<?php
  $name = "Arjun";
  echo "hello {$name}";
?>

Output

PHP - Variable in a string

Conclusion

In this PHP Tutorial, we learned how to include a variable in a string using curly braces notation.