0
0
PHPprogramming~5 mins

String interpolation in double quotes in PHP

Choose your learning style9 modes available
Introduction
String interpolation lets you put variables directly inside double-quoted strings to make messages easier to read and write.
When you want to include a variable's value inside a message without breaking the string.
When building dynamic text like greetings or status messages that change based on data.
When you want cleaner code instead of using many concatenation dots (.) to join strings and variables.
Syntax
PHP
$variable = "value";
$message = "This is a $variable inside a string.";
Only variables inside double quotes get replaced with their values automatically.
Single quotes (' ') treat everything as plain text, so variables won't be replaced.
Examples
Prints Hello, Alice! by inserting the variable $name inside the string.
PHP
$name = "Alice";
echo "Hello, $name!";
Shows how numbers stored in variables can be included in strings.
PHP
$count = 5;
echo "You have $count new messages.";
Using curly braces {} to clearly mark the variable name when adding letters after it.
PHP
$fruit = "apple";
echo "I like {$fruit}s.";
Sample Program
This program shows how to put two variables inside a double-quoted string to print a sentence.
PHP
<?php
$name = "Bob";
$age = 30;
echo "My name is $name and I am $age years old.";
?>
OutputSuccess
Important Notes
Curly braces {} help PHP know exactly where the variable name ends inside the string.
If you use single quotes (' '), variables will not be replaced and will print as plain text.
You can also include array elements like "{$arr[0]}" inside double quotes using curly braces.
Summary
String interpolation lets you put variables directly inside double-quoted strings.
Use double quotes " " to enable interpolation; single quotes ' ' do not work.
Curly braces {} help separate variable names from surrounding text.