PHP › Strings & String Functions
What is the difference between single quoted and double quoted strings in PHP, particularly regarding variable interpolation and escape sequences?
BeginnerAnswer
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, AlexReal-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.