Welcome to a quick tutorial on PHP escape 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.
REAL QUICK SLIDES
TABLE OF CONTENTS
|Download & Notes||Escape Sequence||Useful Bits & Links|
DOWNLOAD & NOTES
Firstly, here is the download link to the example code as promised.
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.
If you spot a bug, please feel free to comment below. I try to answer 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.
All right, let us now get into some simple examples on how to use escape sequence in PHP.
1) STRING BASICS
<?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 // $strA = 'Foo's Bar.'; // (B2) TRYING TO ADD DOUBLE QUOTES // $strB = "Jon says "foo bar"."; // (B3) TRYING TO ADD DOLLAR SIGN // PHP WILL ASSUME IT IS REFERRING TO A VARIABLE // $strC = "Joy says "$foo bar"."; // (B4) ERROR WITH SLASHES // THIS WILL SHOW UP AS D: estile.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.
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
\, end of story.
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 can also be used to represent special characters, we can use them to add tabs and line breaks.
USEFUL BITS & LINKS
That’s all for this project, and here is a small section on some extra links that may be useful to you.
INFOGRAPHIC CHEAT SHEET
REFERENCES & LINKS
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!