Delimiting Strings

PHP strings can be delimited in four different ways. A string is any series of characters enclosed in single (‘ ‘) or double (” “) quotes, or that you create using special heredoc or nowdoc syntax.

Single quoted string

Single quoted strings are defined using the single quote character:

 $user = 'Guest';
 $welcome = 'Hello $user';
 echo $welcome;
 //Hello $user

If you need to use a single quote or backslash within a string, escape them with a backslash:

 $a = 'Someone\'s property';
 echo $a;
 //Someone's property

 $b = 'Trailing backslash\\';
 echo $b;
 //Trailing backslash\

Double quoted string

Double quoted strings behave similarly to strings enclosed in single quotes but they can return the value of another variable and interpret the special characters, such as a new line \n character (see, escape characters):

 $user = 'Guest';
 $welcome = "Hello $user \nThis will print on next line";
 echo $welcome;
  Hello Guest
  This will print on next line

Single vs double quotes string

The double-quoted strings evaluate the value of variables but single-quoted strings store the whole text as is:

 $user = 'Guest';
 echo 'Hello $user';
 //Hello $user

 echo "Hello $user";
 //Hello Guest

Double quoted string evaluates special escape characters but the single-quoted string not:

 echo 'hello \n\n world';
 //hello \n\n world

 echo "hello \n\n world";


Single quoted strings need to escape apostrophe but double-quoted strings not:

 echo 'User\'s profile';
 //User's profile

 echo "User's profile";
 //User's profile

Double quoted strings need to escape double quotes " but single-quoted string not:

 echo 'Who says, "Hello!".';
 //Who says, "Hello!".

 echo "Who says, \"Hello!\".";
 //Who says, "Hello!".

Both single and double-quoted strings need to escape a backslash if followed by a special character:

echo '\Backslash \\\' and apostrophe';
//\Backslash \' and apostrophe

echo "\Backslash and \\\" quotes";
//\Backslash and \" quotes

Heredoc string

The heredoc string begins with the <<< operator followed by an identifier and a new line. The identifier can be any combination of alphanumeric characters or underscores that don’t begin with a digit. To close the heredoc string write the identifier on a new line followed by a semicolon.

$user = 'Guest';

$body = <<<FIRSTPARA
Welcome back "$user". \n\n

echo $body;
//Welcome back "Guest".

Heredoc string behaves identically to double-quoted strings, except that there is no need to escape quotes in the string itself.

Nowdoc String

In PHP 5.3 you can create strings using nowdoc syntax which is similar to heredoc syntax, the only difference is that the delimiter is enclosed within single quotes, and variables are not parsed inside a nowdoc string:

$body = <<<'LABLE'
Welcome back "$user".\n\n

echo $body;
//Welcome back "$user".\n\n

The nowdoc syntax is useful if you want to embed a block of PHP code within your script, without the code being processed at all.

Heredoc vs Nowdoc

Heredoc is equivalent to double-quoted strings:

  • Variable names are replaced with variable values
  • Evaluates special characters

Nowdoc works similarly to single-quoted strings:

  • Variable names remain intact
  • No escaping takes place
  • The string is used entirely as is.

Escaping Quotation Marks

The backslash (\ escape character) is used to remove the characteristics of a special character. Quotation marks can be escaped like this:

echo "This string has a \": a double quote!";
echo 'This string has a \': a single quote!';

One of the convenient features of PHP is the ability to include the value of a variable in a string literal. PHP parses double-quoted strings and replaces variable names with the variable’s value. The following example shows how:

$number = 45;
$vehicle = "bus";
$message = "This $vehicle holds $number people";
$message2 = 'This $vehicle holds $number people';

echo $message;
// prints "This bus holds 45 people"

echo $message2;
// print "This $vehicle holds $number people"

To include backslashes and dollar signs in a double-quoted string, the escaped sequences \\ and \$ can be used.

$var = "a string with a \\ and a \$variable"

The single-quoted string isn’t parsed in the same way as a double-quoted string and can print strings such as:

$var = 'a string with a \ and a $variable'

Joining Strings Together

PHP uses a period, dot, or full stop (.) as concatenation operator for joining strings:

$hello = 'Hello,';
$world = 'World';
echo $hello . $world; // Hello,World

When two strings are joined like this, PHP leaves no gap between them. Don’t be fooled into thinking that adding a space after the period will do the trick. It won’t. You can put as much space on either side of the period as you like; the result will always be the same because PHP ignores whitespace in code. In fact, it’s recommended to leave a space on either side of the period for readability.

To display space in the final output, you must either include a space in one of the strings or insert the space as a string in its own right, like this:

echo $hello . ' ' . $world; // Hello, World

Common Escape Sequences in PHP

As already shown in previous examples, enclosing characters in single quotes or double quotes can create a string literal. Single-quoted strings are the simplest form of string literal; double-quoted strings are parsed to substitute variable names with the variable values and allow characters to be encoded using escape sequences. Single-quoted strings don’t support all the escape sequences, only \' to include a single quote and \\ to include a backslash.

Tab, newline, and carriage-return characters can be included in a double-quoted string using the escape sequences \t\n, and \r, respectively. To include a backslash, a dollar sign, or a double quote in a double-quoted string, use the escape sequences \\\$, or \".

A dollar sign ($) in front of variables
The $ sign is used to insert variables inside literal string values so that the variables are distinguished from the rest of the string, see You need to escape $ character in double-quoted and heredoc strings. For example:

$hw = "Hello world";
echo "The \$hw value is $hw";
//The $hw value is Hello world

\u{} : Inserting Unicode Characters
Introduced in PHP 7. It’s now possible to write Unicode characters easily by using a double-quoted or a heredoc string, without calling any function. For example:

echo "\u{25B6}";
//Prints: ▶

echo "\u{2117}";
//Prints: ℗

echo "\u{00A9}";
//Prints: ©

Escaping apostrophes
Use backslash (\')to escape apostrophe in a single-quote or nowdoc string. For example $a = 'User\'s';

Escaping backslash
Escape backslash if followed by escape character. For example:

echo '\Backslash \\';
#prints: \Backslash \

echo "\Backslash and \\\" quotes";
#prints: \Backslash \" quotes

Line-feed or LF, used as a new line character in Unix and Mac OS X.

Carriage return (CR), it means to return to the beginning of the current line without advancing downward. It was used as a new line character in Mac OS before X.

Carriage return and line-feed (CR+LF), used as a new line character in Windows.

\t horizontal tab
Represent the tab character (tab key on a keyboard).

\v vertical tab
The vertical tab is so rare nowadays. It was used to speed up printer vertical movement.

\e escape
If you make a PHP script which is meant to run from the command line, \e can come in handy when you want to do colors and formatting in terminal emulators.

\f form feed
The page separating character, it forces the printer to eject the current page and to continue printing at the top of another.

Getting Started with PHP:

  1. Introducing PHP
  2. PHP Development Environment
  3. Variable Assignment, Expressions, and Operators
  4. Delimiting Strings
  5. Variable Substitution
  6. Constants