Back to results for “Scope”
PHP › Strings & String Functions

What is the difference between single quoted and double quoted strings in PHP, particularly regarding variable interpolation and escape sequences?

Beginner

Answer

Single quoted strings treat almost everything inside them literally, meaning a variable name written inside a single quoted string is not replaced with its value and most escape sequences are not processed, while double quoted strings support variable interpolation, automatically replacing a variable's name with its actual current value, and also support special escape sequences like a newline character, making double quotes generally more convenient when you need to embed dynamic values or special characters directly within your string.
$name = 'Alex';
echo 'Hello, $name';   // Outputs: Hello, $name
echo "Hello, $name";  // Outputs: Hello, Alex
Real-world example A developer debugging unexpected output realizes a variable was not actually being replaced with its value because the string was written using single quotes instead of double quotes, and switches to double quotes to correctly interpolate the variable.

Common follow-ups: Are there any performance differences between single quoted and double quoted strings?;What common escape sequences are available within double quoted strings?