PHP Escape Sequences

PHP escape sequences are interpolated into strings, enclosed by single/double quotations or heredoc syntax. As its name, it is for escaping characters from being parsed as it is, literally.

Escape sequences are started with escaping character backslash (\) and there are two types of escape sequences. The two types are distinguished based on character following the escape character backslash, that is based on whether it is an alphanumeric or special character.


  • If it is alphanumeric, the escape sequence on the whole including the backslash is given a meaning. For example, it can be \n \r \t etc.
  • If it is a special character, then the character following the backslash is parsed as it is in that location. For example, if we want to include a quote (“) inside an echo statement/string then we should use \”

Escape Sequence Example

For example, this code shows the difference between parsing escape sequences with alphanumeric and non-alpha numeric characters.

echo "PHP Escape Sequences:\n Backslash \\ is used as an escaping character.";

In the above PHP program, we have used two escape sequences \n and \\.

  • \n will be parsed as PHP line break.
  • A \\ backslash followed by backslash will be parsed and a \ (single backslash) will be taken in that position and displayed since it is non-alphanumeric.

Widely used Escape Sequences in PHP

  • \’ – To escape ‘ within single quoted string.
  • \” – To escape “ within double quoted string.
  • \\ – To escape the backslash.
  • \$ – To escape $.
  • \n – To add line breaks between string.
  • \t – To add tab space.
  • \r – For carriage return.

Among the above list of PHP escape sequences, only the first list item is applicable for single quoted string. All other escape sequences will work only with double quoted strings.

This PHP code tutorial was published on November 8, 2013.

↑ Back to Top