PHP Escape Quotes With Detailed Explanation

In this article, we will learn PHP Escape Quotes which are used for escaping a character during the parsing of the string.

In PHP, escape quotes or sequences are escaping characters which started with a backslash (\) then followed by the character which can be a special character or an alphanumeric character.

In addition, Once the characters are alphanumeric it will automatically give a special meaning that represents a line break (\n) and carriage return (\r).

PHP Escape Quotes With Detailed Explanation
PHP Escape Quotes With Detailed Explanation

How do you escape quotes in PHP?

In PHP, to escape quotes or sequences we will use and start with a backslash (\) sign. In addition, escape quotes or sequences are commonly applied to double-quoted strings. While a single-quoted string can only uses escape quotes or sequences for a backslash or a single quote.

The following listed below are some common escape sequences for double-quoted strings.

  • \\ for a backslash
  • \” for a double quote
  • \n for a new line
  • \t for a tab
  • \$ to render a dollar sign instead of expanding the variable

For example:

"\"What kind of \$ browser use?\"\n\tGoogle Chrome Browser!"

Output:

What kind of browser use?
Google Chrome Browser!

What is PHP Addslashes?

The addslashes() function is a built-in PHP which returns a string with a backslash of predefined characters in front of single quote and backslash.

The following listed below are predefined characters.

  • single quote (‘)
  • double quote (“)
  • backslash ()

Single Quoted In PHP

The single quoted is the simplest and easiest way to define a string. You just need to enclose it in a single quotes (”) to escape special characters.

For instance, you can use this single quote if you really want the string in PHP to be exactly as it is written and output.

In addition, All the escape quotes or sequences such as \r or \n, will be printed out or output as defined instead of possess any special meaning. The single quotes are usually faster in some cases of testing and debugging

The special case allows you to display a literal (single-quote), then it will escape with a backslash (\) and in some instances that you want to display a backslash, you can easily escape it with an additional backslash(\\).

For example:

<?php

// characters are single quote
echo ‘I am Glenn Magada Azuelo a software developer.’;
echo “\n”;
//use of \ to display string
Echo ‘it will be interesting to know about the string and escape quotes.’;
Echo “\n”;
//to escape the backslash within the string
Echo ‘x \\ is named as backslash’;
Echo “\n”;

?>

Output:

I am Glenn Magada Azuelo a software developer.
it will be interesting to know about the string and escape quotes.
x \\ is named as backslash

Double Quoted in PHP

The double quoted is symbolized with ("") once the string is enclosed with double quotes the PHP will automatically interpret the following escape sequences as a special characters.

Escaped characters

SequenceMeaning
\nlinefeed (LF or 0x0A (10) in ASCII)
\rcarriage return (CR or 0x0D (13) in ASCII)
\thorizontal tab (HT or 0x09 (9) in ASCII)
\vvertical tab (VT or 0x0B (11) in ASCII)
\eescape (ESC or 0x1B (27) in ASCII)
\fform feed (FF or 0x0C (12) in ASCII)
\\backslash
\$dollar sign
\"double-quote
\[0-7]{1,3}the sequence of characters matching the regular expression is a character in octal notation, which silently overflows to fit in a byte (e.g. “\400” === “\000”)
\x[0-9A-Fa-f]{1,2}the sequence of characters matching the regular expression is a character in hexadecimal notation
\u{[0-9A-Fa-f]+}the sequence of characters matching the regular expression is a Unicode codepoint, which will be output to the string as that codepoint’s UTF-8 representation

For example:

<?php

// A Simple String
echo “I am Glenn Magada Azuelo a software developer.”;
echo “\n”;
Echo “it will be interesting to know about the string and escape quotes.”;
Echo “\n”;
//variables are included directly and parse error
$string = ‘xyx’;
echo “the word is $string”;

?>

Output:

I am Glenn Magada Azuelo a software developer.
it will be interesting to know about the string and escape quotes.
the word is xyx

Nowdoc in PHP

The nowdoc is a single quoted string which is define similarly to a heredoc, but the difference is there is no parsing done inside the nowdoc.

In addition, the construct and escape using a backslash are perfect for embedding PHP code or other blocks which is large of text without the need of escaping the string.

For example:

echo <<<’END_OF_HTML’
$hello this is a test by Glenn Magada Azuelo {$a->test}
END_OF_HTML;

Output:

$hello this is a test by Glenn Magada Azuelo {$a->test}

Heredoc in PHP

The heredoc is used to delimit a string with a syntax of (<<<). After this operator, the identifier was provided and a new line quote character. Then the string itself was followed and the same identifier was to close the entire quotation to escape the quotation marks in PHP.

Furthermore, the closing identifier will begin on the first line of the column characters with single and double quotes.

For example:

<?php
echo <<<EOT
Glenn Magada Azuelo
EOT;

Output:

Glenn Magada Azuelo

Summary

This article discusses the PHP Escape Quotes and also tackles how you escape quotes, what is PHP Addslashes, single-quoted, double quoted, nowdoc, and heredoc.

I hope this lesson has helped you learn a lot. Check out my previous and latest articles for more life-changing tutorials that could help you a lot.

Leave a Comment