Welcome to a quick tutorial on PHP escape characters and sequences. Trying to figure out how to add slashes and quotation marks to a string? Well, using escape sequences is the answer.
Escape sequences start a backslash
\, followed by a few characters. They are used to represent special characters that are otherwise impossible to enter within the script. A few common examples:
That covers the basics, but read on for more examples!
ⓘ I have included a zip file with all the example source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.
TLDR – QUICK SLIDES
TABLE OF CONTENTS
DOWNLOAD & NOTES
Firstly, here is the download link to the example code as promised.
QUICK NOTESIf you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.
EXAMPLE CODE DOWNLOAD
Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
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.
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!