String (text) manipulation in PHP is extremely important. It’s used in various situations, such as handling user input, formatting and displaying data from databases, or dynamically generating HTML. This section will guide you through how to work with strings in PHP.
How to Write Text
First, let’s go over the basics of writing strings in PHP. In PHP, strings are represented using single quotes or double quotes as shown below:
- Single quotes (
''
) - Double quotes (
""
)
Since PHP treats these differently, it’s important to understand how string handling changes depending on which one you use.
Ways to Write Strings
To assign strings, you typically use one of the two following methods:
<?php// Single quotes
$text1 = 'Hello';
// Double quotes
$text2 = "Hello";
?>
Although they may look similar, the internal handling differs slightly, so let’s move on to understanding the difference between quote types.
Quotes: Differences Between Single and Double Quotes
To properly understand how strings work, it’s important to grasp the differences between single and double quotes.
Single Quotes
- Do not expand variables: Writing
$variable
inside the string will be treated as-is. - Fewer characters need escaping: Generally, only single quotes and backslashes need to be escaped.
- Interpreted literally: Special characters (like
\n
) are displayed as they are.
Sample Code:
<?php
$name = 'Taro';
// Variables inside single quotes are not expanded
echo 'Hello, $name <br>';
// Escape single quotes inside single quotes
echo 'I\'m a PHP developer!';
?>
Output:

Double Quotes
- Expands variables: When
$variable
is included, its value is automatically inserted. - Interprets special character sequences: Escape sequences like
\n
(newline) or\t
(tab) can be used.
Sample Code:
<?php
$name = 'Taro';
// Variables are expanded in double quotes
echo "Hello, $name <br>";
// Newline escape sequence example
echo "Line 1\nLine 2";
?>
Output:


Escaping Strings
In PHP, strings are written within ''
(single quotes) or ""
(double quotes). If the same quote or a backslash \
appears inside the string, it may be misinterpreted as the end of the string or as a special character, causing a syntax error.
To prevent this, use “escaping” by adding a backslash \
before these characters to tell PHP to treat them as literal characters.
Sample Code:
<?php
// To display double quotes inside double quotes
echo "He said, \"Hello!\"";
// To display single quotes inside single quotes
echo 'It\'s a wonderful day!';
?>
Output:

Using escaping like this ensures the string is handled correctly.
Special Characters in Double Quotes
Inside double quotes, PHP can interpret special characters (escape sequences) such as newlines and tabs.
Sample List of Special Characters in Double Quotes
\n
: Newline\t
: Horizontal tab\\
: Backslash itself\"
: Display a double quote
Sample Code:
<?php
echo "String with newline\nThis is on a new line<br>";
echo "String with a tab\tincluded<br>";
echo "Displaying a backslash: \\<br>";
echo "Displaying double quotes: \"<br>";
?>
Output:


Using these allows you to easily format text output for HTML or logs.
Heredoc Syntax
Heredoc syntax allows you to write long, multi-line strings clearly. You use <<<
followed by an identifier (e.g., EOD
), and everything up to that identifier again at the start of a line is treated as a string.
Basic Usage:
<?php
$text = <<<EOD
You can write
multi-line strings directly.
You can also include "double quotes" inside the string.
EOD;
echo $text;
?>
Output (source code):

Like double quotes, Heredoc also supports variable expansion and escape sequence interpretation.
Variable Expansion in Heredoc
In Heredoc, writing $variable
inside the string automatically expands the variable.
<?php
$name = 'Hanako';
$message = <<<EOT
Hello, $name.
It's a nice day today.
EOT;
echo $message;
// Output:
// Hello, Hanako.
// It's a nice day today.
?>
This mechanism allows flexible string generation by embedding variables within HTML or long texts.
Nowdoc Syntax
Nowdoc syntax is similar to Heredoc, but treats the string using single quote rules, meaning no variable expansion or escape sequence interpretation. It is useful when displaying strings like code examples exactly as written.
Example Usage:
<?php
$text = <<<'EOD'
In Nowdoc syntax, $name is not expanded
and is displayed as $name.
EOD;
echo $text;
// Output:
// In Nowdoc syntax, $name is not expanded
// and is displayed as $name.
?>
Nowdoc outputs the exact string you wrote. Compared to Heredoc, it’s useful when you want to avoid unnecessary replacements or interpretations.
This concludes the basics of string manipulation in PHP. Choose between single quotes, double quotes, Heredoc, or Nowdoc depending on readability and required functionality like variable expansion. Beginners should start with simple methods and adopt Heredoc or Nowdoc as needed.