PHP TUTORIALPHP TutorialPHP InstallationPHP Hello WorldPHP Basic SyntaxPHP CommentsPHP VariablesPHP Variable ScopePHP ConstantsPHP StringsPHP OutputPHP Data TypesPHP Type CastingPHP OperatorsPHP ConditionalsPHP Shorthand ConditionalsPHP LoopsPHP Loop Control StructuresPHP FunctionsPHP String FunctionsPHP ArraysPHP Superglobal VariablesPHP in HTMLPHP AdvancedPHP Include and RequirePHP HTTP & HTTPSPHP RegexRegex IntroductionRegex PCRE SyntaxPHP PREG FunctionsPHP FormsPHP Forms IntroductionPHP Forms CreatingPHP Forms SecurityPHP Forms ValidationPHP Forms Required InputsPHP Forms StickyPHP Forms Advanced ValidationPHP Forms FinishingPHP OOPPHP OOP IntroductionPHP OOP ClassesPHP OOP PropertiesPHP OOP ObjectsPHP OOP MethodsPHP OOP $this KeywordPHP OOP Constructors and DestructorsPHP OOP VisibilityPHP OOP InheritancePHP OOP Abstract Classes and MethodsPHP OOP InterfacesPHP OOP TraitsPHP OOP ConstantsPHP OOP StaticPHP OOP NamespacesPHP OOP Autoloading

PHP Strings

A string is any finite sequence of characters (letters, numerals, symbols, punctuation marks, etc.)

Declaring Strings

There are three ways.

Single Quoted

The easiest way to declare a string is by enclosing it by single quotes. Character: '

$string = 'This is my first string';

What if you needed to add a single quote inside a single quoted string? Just escape that character with a back slash. \'

# This is the Hyvor's PHP Tutorial
$string = 'This is the Hyvor\'s PHP Tutorial';

What if you needed to have \' in a single quoted string? Use \\ to escape the \ character. And, as in the last example, \' to escape the ' character.

# Backslash and Quote: \'
$string = 'Backslash and Quote: \\\'';

Escaping is used to reduce ambiguity and give special meaning when using quotes. These characters are called escape sequences.

Double Quoted

Strings can be declared enclosed by double quotes. Character: ".

In single quotes, only \\ and \' had a special meaning. But, in double quotes, there are more escape sequences.

Escape SequenceMeaning
\nLine Break
\rCarriage Return
\tTab Space
\$Dollar Sign
\"Double Quotes
All escape characters starts with a backslash \.

PHP Double Quoted String Example

echo "Hello World";
echo "<br>";
echo "\"Hello World\"";
echo "\n\tHello World\n";

Run Example ››

Tip: The reason we have echoed out the output from PHP inside <pre> tags is those tags allows you to show the text as it is in the browser. (By default, HTML ignores extra white spaces)

In the table, you may notice the $ (dollar) sign. Why is it there? The reason is that we can add variables inside double quoted texts

Variables within Double Quoted Strings

When you add a variable within a double quoted string, PHP will parse it.

PHP Variables in Double Quoted String

$name = "Hyvor";
echo "My name is $name";

Run Example ››

If we needed to add a s character in front of a variable in the same double quoted string, we will need to wrap the variable with curly braces.

PHP Variables in Double Quoted String - Complex Syntax

$fruit = 'Apple';
// echo "$fruits"; # there's no variable $fruits
echo "{$fruit}s";

Run Example ››

PHP Single vs. Double Quoted Strings

Will be treated literally, displays exactly what you typeWill be interpreted
Does not work with escape sequences, except \' and \\Works with escape sequences
Variables inside are not parsedVariables inside are parsed

// both works
echo "Hello <br>";
echo 'Hello <br>';

// escape sequences are not parsed in single quotes
echo "\"World\" <br>"; // outputs "World"
echo '\"World\" <br>'; // outputs \"World\"

// variables are not parsed in single quotes
$hello = "Hello";
echo "$hello World <br>"; // outputs Hello World
echo '$hello World <br>'; // outputs $hello World

Run Example ››

Doc Syntax

Doc Syntax is the next way of declaring a string. It has two types which each works like single quoted and double quoted strings.

Doc Syntax

  • Starts with <<<.
  • Then, add an identifier in the same line enclosed with single quotes or double quotes. (Single quotes for nowdoc and double quotes for heredoc)
  • Write the string in a new line. (It can be multiline)
  • End with the identifier and a ; (Semi colon) in a new line. (First letter of the identifier should be the first character in that line. It is a must!)
Identifier is any valid PHP label. The same naming rules you learned in the constants chapter applies to this identifier. (Should start with a non-digit character or underscore and must only contain alpha-numeric characters and underscores)


Nowdoc string is declared using the doc syntax and single quotes for the identifier.

PHP Nowdoc Example

$string = <<<'MYSTRING'
I'm a nowdoc string
Did you notice that I can write '' without escaping?
I don't have special meaning for \n, \r and \t
I do not parse variables $name
echo '<pre>' . $string . '</pre>';

Run Example ››

Nowdoc strings behaves like single quoted strings. There is no special meaning for escape sequences. Variables are not parsed.


Heredoc string is declared using the doc syntax and double quotes (or without quotes) for the identifier.

PHP Heredoc Example

$name = 'Hyvor';
$string = <<<STR
I'm a heredoc
I parse variables. (I love $name)
\t \\t adds a tab space. That means I accept escape sequences

echo '<pre>' . $string . '</pre>';

Run Example ››

Heredoc strings behaves like double quoted strings. It converts escape sequences to their real meaning. And, variables will be parsed.

The best thing about doc syntax is that we don't have to escape quotes inside them. This makes writing HTML, XML, SQL, etc. strings easy.
Visit PHP Help group to get some help from experts.
Profile Picture
Supun Kavinda
I'm the Founder of Hyvor, Web Developer, Physics Lover, Flutist, and a Table Tennis Player.
My Websites