In PHP, a string is a sequence of characters, such as letters, numbers, or symbols, wrapped together to form text.
PHP provides different ways to write strings in your code, and understanding the differences between them is essential for writing clean script logic.
Single-quoted strings are the simplest way to define text.
PHP treats everything inside single quotes completely literally.
<?php
$name = 'Alice';
echo 'Hello, $name\n';
// Outputs literally: Hello, $name\n
Key Behaviors: No Variable Parsing: If you place a variable inside single quotes, PHP will not look up its value. It outputs the literal characters (e.g., $name).
Minimal Escape Sequences: Standard escape characters like \n (new line) or \t (tab) do not work inside single quotes.
They are printed literally as a backslash and a letter. The only characters you need to escape are a literal single quote (') and a backslash (\).
Double-quoted strings are much more powerful because PHP automatically evaluates special escape sequences and variables hidden inside them.
<?php
$name = "Alice";
echo "Hello, $name!\nWelcome back.";
// output:
// Hello, Alice
// Welcome back.
Key Behaviors:
Variable Interpolation - PHP parses the string and swaps out any variable name with its actual contents.
Escape Sequences Allowed - Special formatting commands like \n (which inserts a real line break) or \t (which inserts a tab space) will be processed correctly instead of being printed literally.
When inserting a variable into a double-quoted string right next to other text, PHP can get confused about where the variable name ends.
To make your intentions explicit, wrap the variable in curly braces {}:
<?php
$fruit = "Apple";
// This crashes or checks for a variable named $fruits
// echo "I have two $fruits";
// Using curly braces cleanly isolates the variable name
echo "I have two {$fruit}s.";
If you need to write long blocks of text spanning multiple lines (like an email template or a snippet of HTML), using standard quotation marks can quickly make your code messy.
PHP offers two special identifiers for multi-line blocks.
A Heredoc block behaves exactly like a double-quoted string—it parses variables and escape sequences—but allows you to write across multiple lines cleanly.
It is initiated using <<< followed by a custom text delimiter (often EOD or TEXT), and closed by repeating that exact same delimiter on its own line.
<?php
$user = "Bob";
$emailTemplate = <<<EOD
Hello $user,
Thank you for registering an account with us.
Your profile is now active!
EOD;
echo $emailTemplate;
Introduced to complement Heredoc, a Nowdoc block behaves exactly like a single-quoted string.
It reads everything literally and will not parse variables or escape sequences.
You define it exactly like a Heredoc, but you must wrap the opening identifier label in single quotes ('EOD').
<?php
$user = "Bob";
$rawCodeSnippet = <<<'CODE'
To display a message in PHP, we write:
echo "Hello, $user";
CODE;
echo $rawCodeSnippet;
// Outputs the text exactly as written, displaying the literal text "$user"
Use Single Quotes ('message') when your text is plain and contains no variables or complex format layout, as it keeps your code explicit and literal.
Use Double Quotes ("hello $user") whenever you want to seamlessly blend variables directly into your sentences or require structural formatting like \n.
Use Heredoc/Nowdoc exclusively when dealing with multi-line paragraphs or blocks of layout templates to avoid cluttering your code with escaping backslashes.