Basic Explanation
In PHP, text is stored in the string type. You can define strings using:
- single quotes: 'text'
- double quotes: "text"
- With double quotes, PHP performs interpolation (replaces variables with their values) and recognizes escape sequences (e.g., newline
\n
). - With single quotes, the text is treated almost literally (no interpolation and no most escape sequences).
Single vs Double Quotes — Key Differences
- Variable interpolation
$variable
is replaced with its value.
- Escape sequences
\\
(backslash) and \'
(apostrophe). Others like \n
or \t
remain literal.
* "..." — recognizes: \n
(newline), \r
, \t
(tab), \v
, \e
, \f
, \\
, \"
, \$
, as well as hexadecimal and Unicode values (e.g., \x41
, \u{1F600}
).
- Arrays and objects in interpolation
- Performance
- When to use?
\n
.
- Additionally: for multi-line text, learn heredoc and nowdoc:
PHP Code Examples
1) Interpolation vs literal text
<?php
$name = 'Alice';
// Double quotes — interpolation
echo "Hello, $name!\n"; // Output: Hello, Alice! + newline
// Single quotes — no interpolation
echo 'Hello, $name!' . "\n"; // Output: Hello, $name!
2) Escape sequences: \n and \t
<?php
// Double quotes interpret \n and \t
echo "Line 1\nLine 2\t<- tab\n";
// Single quotes treat \n literally
echo 'Line 1\nLine 2\t<- tab' . "\n";
3) Concatenation vs interpolation
<?php
$name = 'John';
$age = 30;
// Interpolation
echo "My name is $name and I am $age years old.\n";
// Concatenation
echo 'My name is ' . $name . ' and I am ' . $age . " years old.\n";
4) Arrays and objects in interpolation
<?php
$user = [
'name' => 'Eve',
'roles' => ['admin', 'editor']
];
$person = (object) ['name' => 'Mark'];
// Numeric index may work without braces:
echo "First role: $user[roles]\n";
// ⚠️ Wrong! PHP treats 'roles' as a constant name.
// Correct way:
echo "First role: {$user['roles'][0]}\n"; // Output: admin
// Objects — always use braces for clarity:
echo "User: {$person->name}\n"; // Output: Mark
⚠️ Incorrect example:
// echo "User: $user['name']"; // ❌ parse error
Correct:
echo "User: {$user['name']}";
5) Escaping quotes
<?php
// In single quotes, escape apostrophe (\') and backslash (\\)
echo 'It\'s a backslash: \\.' . "\n";
// In double quotes, escape double quote (\") and backslash (\\)
echo "This is a \"quote\" and a backslash: \\\n";
// Dollar sign without interpolation
echo "Price: \$10\n"; // Output: Price: $10
6) Windows paths and special characters
<?php
// In double quotes, sequences like \n are interpreted:
echo "C:\\new\\folder\\file.txt\n";
// In single quotes, \n is literal — often simpler:
echo 'C:\new\folder\file.txt' . "\n";
7) Multi-line text: heredoc and nowdoc
<?php
$name = 'Kate';
// heredoc — behaves like double quotes
$heredoc = <<<TXT
Hello, $name!
This is multi-line text.
TXT;
// nowdoc — behaves like single quotes
$nowdoc = <<<'TXT'
Hello, $name!
This is multi-line text.
TXT;
echo $heredoc . "\n"; // Output contains: Hello, Kate!
echo $nowdoc . "\n"; // Output contains: Hello, $name!
Best Practices and Common Mistakes
Best Practices
- Use single quotes for simple, literal strings.
- Use double quotes when you need interpolation or escape sequences (
\n
,\t
). - For arrays/objects in interpolation, always use curly braces:
{$arr['key']}
,{$obj->prop}
. - Avoid overly complex interpolations — use concatenation if readability suffers.
- Be consistent with style (PSR-12 coding standards).
- For HTML/JSON/SQL:
htmlspecialchars
.
* JSON: use json_encode
instead of manual concatenation.
- Use heredoc/nowdoc for long, multi-line text.
Common Mistakes
- Expecting
\n
or\t
to work in single quotes (they don’t). - Forgetting curly braces for string keys in arrays inside double quotes.
- Improperly escaping quotes.
- Confusing backslashes in Windows paths when using double quotes.
- Building SQL/HTML directly with interpolated variables — risks SQL Injection and XSS.
- Overusing interpolation instead of simple concatenation.
Summary
- Both '...' and "..." create strings in PHP, but:
\'
and \\
are recognized.
* "..." — supports interpolation and escape sequences.
- Use curly braces for array keys and object properties inside interpolated strings.
- Choose based on readability and intent.
- Always keep security in mind: prepared statements, HTML escaping, json_encode.
Mini Quiz
- What does this output?
$name = 'Ola';
echo 'Hello, $name!';
➡️ Hello, $name!
- What does this output?
$name = 'Ola';
echo "Hello, $name!";
➡️ Hello, Ola!
- Which is correct for an array key?
echo "User: $user['name']";
- B)
echo "User: {$user['name']}";
- C)
echo "User: $user[name]";
- True/False: In single quotes,
\n
creates a new line.
- What must you escape in double quotes to display
$
and"
?
\$
and \"
- What does this output?
echo "A\tB\nC";
➡️ A, tab, B, newline, C
- Which is safer and clearer for object properties?
"Name: $user->name"
- B)
"Name: {$user->name}"
- How to correctly display Windows path C:\new\file.txt in double quotes?
You now have solid foundations for working with strings in PHP. In upcoming tasks, choose the appropriate quotes depending on whether you need interpolation and escape sequences or literal text. This ensures your code remains readable, secure, and maintainable.