PHP Quotes — Single vs. Double

← Back to Index


Example 1: Double Quotes

$name = "Alice";
$double = "Hello, $name! Welcome to PHP.";
echo $double;
    

Output: Hello, Alice! Welcome to PHP.


Example 2: Single Quotes

$name = "Alice";
$single = 'Hello, $name! Welcome to PHP.';
echo $single;
    

Output: Hello, $name! Welcome to PHP.


Explanation

In PHP, double quotes allow variable interpolation, meaning any variable written inside a double-quoted string will be automatically replaced with its actual value. For example, "Hello, $name" outputs Hello, Alice because PHP replaces the variable with its value. Single quotes treat every character literally — PHP does not parse variables inside single-quoted strings, so 'Hello, $name' outputs the raw text Hello, $name instead of the variable's value. Double quotes also support escape sequences like \n (newline) and \t (tab), while single quotes only support \' and \\. Use single quotes when you do not need variable parsing as they are slightly faster to process.

INFOST 440 — Activity 3 — Nyla Broughton