Strings
Learn how to create, quote, concatenate, and index PHP strings, including the difference between single and double quotes.
Introduction
Strings are sequences of characters — text — and they are one of the most frequently used data types in any PHP script, from displaying a greeting to building an HTML page. PHP gives you more than one way to create a string, and the way you choose changes how PHP treats the text inside it.
This lesson covers how to create and work with strings themselves. PHP's large library of built-in string functions — for searching, replacing, splitting, and transforming text — gets its own dedicated lesson right after this one.
- How to create strings with single and double quotes.
- The key difference: double quotes interpolate variables and escape sequences, single quotes mostly do not.
- How to concatenate strings with the . operator.
- How to access individual characters using $str[0] indexing.
- A brief introduction to heredoc and nowdoc syntax.
Creating Strings
The simplest way to create a string in PHP is to wrap text in either single quotes (') or double quotes ("). Both produce a string, but they behave differently once variables or special characters are involved.
<?php
$single = 'Hello, World!';
$double = "Hello, World!";
echo $single;
echo "\n";
echo $double;
?>Hello, World!
Hello, World!Single vs Double Quotes
The difference only becomes visible once a variable or an escape sequence appears inside the string. Double-quoted strings interpolate variables (substituting in their value) and interpret escape sequences like \n for a newline. Single-quoted strings treat almost everything literally.
<?php
$name = "Amol";
echo "Hello, $name!\n"; // Double quotes: variable interpolated, \n becomes a newline
echo 'Hello, $name!\n'; // Single quotes: printed literally, no interpolation
?>Hello, Amol!
Hello, $name!\n| Feature | Single Quotes '...' | Double Quotes "..." |
|---|---|---|
| Variable interpolation | No (printed literally) | Yes ($name is replaced with its value) |
| Escape sequences (\n, \t, etc.) | Mostly no (only \\ and \' work) | Yes |
| Performance | Marginally faster (no parsing needed) | Marginally slower |
Variable Interpolation
Interpolation means a variable's value is automatically substituted into a double-quoted string without needing to concatenate it manually. For array elements or object properties, curly braces can be used to make the boundaries of the expression explicit.
<?php
$name = "Amol";
$age = 25;
echo "Name: $name, Age: $age\n";
$user = ["name" => "Priya"];
echo "Welcome, {$user['name']}!\n"; // curly braces clarify the expression
?>Name: Amol, Age: 25
Welcome, Priya!Escape Sequences
Escape sequences let you include special characters — like a newline or a literal quote — inside a string. They are only interpreted inside double-quoted strings (with the exception of \\ and \', which also work inside single quotes).
| Sequence | Meaning |
|---|---|
| \n | Newline |
| \t | Tab |
| \\ | A literal backslash |
| \" | A literal double quote (inside a double-quoted string) |
| \$ | A literal dollar sign (prevents interpolation) |
<?php
echo "Line one\nLine two\n";
echo "She said \"hello\".\n";
echo "Price: \$100\n"; // \$ prevents interpolation attempt
?>Line one
Line two
She said "hello".
Price: $100String Concatenation
You already saw this in the Operators lesson: the . operator joins strings together, and .= appends a string onto an existing variable.
<?php
$first = "Code";
$second = "Journey";
$combined = $first . $second;
echo $combined; // "PrograMinds"
$combined .= "!";
echo $combined; // "PrograMinds!"
?>PrograMinds
PrograMinds!Accessing Characters by Index
A PHP string can be treated like an array of characters. Square-bracket notation, $str[0], accesses the character at a given position, starting from index 0. Negative indexes count backward from the end of the string.
<?php
$word = "PHP";
echo $word[0]; // "P"
echo $word[1]; // "H"
echo $word[2]; // "P"
echo $word[-1]; // "P" (last character)
echo strlen($word); // 3 (a preview of string functions - covered in depth next lesson)
?>PHP
P3While you can read $str[0], and even assign to it like $str[0] = "J", strings are still fundamentally different from arrays — most transformations (uppercasing, replacing, splitting) are done through dedicated string functions rather than manual index manipulation.
Heredoc and Nowdoc
For strings spanning multiple lines, especially ones mixing lots of text and variables, PHP offers heredoc syntax — introduced with <<<IDENTIFIER — which behaves like a double-quoted string (interpolating variables) but without needing to escape every quote inside it. Nowdoc, written with <<<'IDENTIFIER' (quoted), behaves like a single-quoted string instead, with no interpolation at all.
<?php
$name = "Amol";
$message = <<<EOT
Hello, $name!
This is a multi-line string.
It supports interpolation just like double quotes.
EOT;
echo $message;
?>Hello, Amol!
This is a multi-line string.
It supports interpolation just like double quotes.Nowdoc works identically but with single quotes around the opening identifier, and treats its contents literally, with no variable interpolation — the closing identifier still must appear on its own line, matching the indentation of the surrounding code.
<?php
$text = <<<'EOT'
Hello, $name!
This is treated literally - no interpolation.
EOT;
echo $text;
?>Hello, $name!
This is treated literally - no interpolation.Common Mistakes
- Expecting single-quoted strings to interpolate variables — they do not, aside from \\ and \'.
- Forgetting that array-key interpolation inside double quotes without braces can behave unexpectedly with string keys.
- Assuming strings are freely mutable like arrays for operations beyond single-character reads or writes.
- Mismatching the indentation of a heredoc/nowdoc closing identifier, which causes a parse error.
Best Practices
- Use single quotes for plain text with no variables, since it is marginally faster and avoids accidental interpolation.
- Use double quotes when you need variable interpolation or escape sequences.
- Use curly braces ({$var}) when interpolating array elements or object properties to avoid ambiguity.
- Reach for heredoc when building long, multi-line strings that still need interpolation.
Frequently Asked Questions
Are single quotes always faster than double quotes?
Marginally, since PHP does not need to scan a single-quoted string for variables or escape sequences. In practice this difference is negligible and should rarely drive your choice.
Can I modify a specific character in a string?
Yes — you can assign directly to an index, like $str[0] = "J";, which replaces just that character in place.
What is the difference between heredoc and nowdoc?
Heredoc (<<<EOT) behaves like a double-quoted string and interpolates variables; nowdoc (<<<'EOT') behaves like a single-quoted string and does not interpolate anything.
Where can I learn more string functions, like uppercasing or searching text?
The very next lesson, String Functions, is dedicated entirely to PHP's built-in string function library.
Key Takeaways
- Strings can be created with single quotes (') or double quotes (").
- Double-quoted strings interpolate variables and interpret escape sequences; single-quoted strings mostly do not.
- The . operator concatenates strings; .= appends to an existing string variable.
- Individual characters can be accessed with $str[0] indexing.
- Heredoc (<<<EOT) and nowdoc (<<<'EOT') support clean multi-line strings, with and without interpolation respectively.
Summary
Strings are everywhere in PHP, and knowing exactly how single quotes, double quotes, interpolation, and concatenation behave will save you from a lot of subtle output bugs. With the fundamentals of creating and combining strings in place, the next lesson dives into PHP's extensive built-in string function library for actually transforming and analyzing text.
- You can create strings with both single and double quotes.
- You understand exactly when interpolation and escape sequences apply.
- You can concatenate strings and access individual characters by index.
- You have seen a first look at heredoc and nowdoc syntax.