PHP Escape Characters & Sequences Explained (Simple Examples)

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:

  • \\ Backslash
  • \" Double quote
  • \' Single quote
  • \$ Dollar sign
  • \r Carriage return
  • \n Newline
  • \t Tab

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 NOTES

If 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.

 

 

ESCAPE CHARACTER/SEQUENCE

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

1-basic.php
<?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

2-escape.php
<?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

3-char.php
<?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

PHP Escape Characters (Click To Enlarge)

 

THE END

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!

Leave a Comment

Your email address will not be published. Required fields are marked *