Welcome to a quick tutorial on PHP escape characters and sequences. So you want to add slashes and quotation marks to a string? Well, using escape sequences is the answer.
Escape sequences are used to represent special characters that are impossible to enter within the script. They start a backslash
\, followed by one or more characters. The common escape characters are:
That covers the basics, but read on for more examples!
TLDR – QUICK SLIDES
Fullscreen Mode – Click Here
TABLE OF CONTENTS
All right, let us now get into some simple examples of how to use escape characters and sequences in PHP.
EXAMPLE 1) THE PROBLEM WITH PHP STRINGS
<?php // (A) STRINGS CAN BE DEFINED WITH SINGLE OR DOUBLE QUOTES $strA = 'Foo'; $strB = "$strA Bar"; echo $strA; echo $strB; // (B) THE PROBLEMS // (B1) TRYING TO ADD SINGLE QUOTES - PARSE ERROR // $strA = 'Foo's Bar.'; // (B2) TRYING TO ADD DOUBLE QUOTES - PARSE ERROR // $strB = "Jon says "foo bar"."; // (B3) TRYING TO ADD DOLLAR SIGN // PHP WILL ASSUME IT IS REFERRING TO $FOO VARIABLE // $strC = "Joy says "$foo bar"."; // (B4) ERROR WITH SLASHES // THIS WILL SHOW UP AS D: est↑ile.txt // $strD = "D:\test\file.txt";
A quick recap – Strings can be defined using either single or double quotes. But the problem comes when we actually try to add quotes inside a string or use “reserved characters” such as dollar and slash.
EXAMPLE 2) ESCAPE CHARACTERS
<?php // (A) ESCAPE SINGLE QUOTE $strA = 'Foo\'s Bar.'; echo $strA . "<br>"; // (B) ESCAPE DOUBLE QUOTES $strB = "Jon says \"foo bar\"."; echo $strB . "<br>"; // (C) ESCAPE DOLLAR SIGN $strC = "Joy says \"\$foo bar\"."; echo $strC . "<br>"; // (D) ESCAPE SLASHES $strD = "D:\\test\\file.txt"; echo $strD . "<br>";
That’s right, fixing the problems are as easy as escaping the characters. Just add a slash in front
\ of the “offending character”.
EXAMPLE 3) SPECIAL CHARACTERS
<?php echo "First\r\n"; echo "\tSecond\r\n"; echo "\tThird";
First Second Third
Escape characters are not just used to escape quotes, slashes, and the dollar sign. They are also be used to represent special characters. There are way too many to list here, I will just leave a link below if you are interested.
DOWNLOAD & NOTES
Here is the download link to the example code, so you don’t have to copy-paste everything.
SORRY FOR THE ADS...
But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.
EXAMPLE CODE DOWNLOAD
Click here for the source code on GitHub gist, just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
EXTRA BITS & LINKS
That’s all for this project, and here is a small section on some extra links that may be useful to you.
REFERENCES & LINKS
INFOGRAPHIC CHEAT SHEET
Thank you for reading, and we have come to the end of this guide. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!