Hudzilla.org - the homepage of Paul Hudson
Contents > Introducing PHP > How PHP is written Wish List | Report Bug | About Me ]

2.6.2     Escape sequences

This is NOT the latest copy of this book; click here for the latest version.

You can achieve the same effect in double-quoted strings by using the escape character, which, in PHP, is a backslash \.

Escape sequences, the combination of the escape character \ and a letter, are used to signify that the character after the escape character should be treated specially. For example, if you wanted to have the string "And then he said, "That is amazing!", which was true", you would need escape characters because you have double quotes inside double quotes. Here is a list of the escape sequences in PHP:

\"

Print the next character as a double quote, not a string closer

\'

Print the next character as a single quote, not a string closer

\n

Print a new line character (remember our print statements?)

\t

Print a tab character

\r

Print a carriage return (not used very often)

\$

Print the next character as a dollar, not as part of a variable

\\

Print the next character as a backslash, not an escape character

Here is a code example of these escape sequences in action:

<?php
    $MyString
= "This is an \"escaped\" string";
    
$MySingleString = 'This \'will\' work';
    
$MyNonVariable = "I have \$zilch in my pocket";
    
$MyNewline = "This ends with a line return\n";
    
$MyFile = "c:\\windows\\system32\\myfile.txt";
?>

It is particularly common to forget to escape Windows-style file system paths properly, but as you can see, it is simply a matter of adding more backslashes in there. If you were to print $MyFile, you would get this:

c:\windows\system32\myfile.txt

This is because the escape characters are just to make sure PHP can read the string correctly - once the data has been read, it is converted into the original format.

Along the same lines, escape sequences only work in double-quoted strings - if you type 'Hello!\n\n\n', PHP will actually print out the characters \n\n\n rather than converting them to new lines. It is important to note that escape characters are just one character in files themselves, however they are represented as two in PHP because they cannot physically be typed using your keyboard.





<< 2.6.1 Whitespace   2.6.3 Heredoc >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
JackS jack42779@yahoo.com - 29 Aug 2008

I think if you check the \n works in javascript, <br /> works in html. If the PHP was embeded in a javascript code command it probably would work. But with PHP you do not need the javascript.

Regards,
jack {;-)

sudhashen@gmail.com - 29 Aug 2008

Great work.Thanks for making this resource available.Please could you give a newbie explanation as to why <br> has to be added to view line breaks in a browser? Why is this not mentioned your notes for the above? My assumption is that \n should show a new line as indicated when testing - however this only shows when showing the source in the browser.

sudhashen@gmail.com - 29 Aug 2008

Great work.Thanks for making this resource available.Please could you give a newbie explanation as to why <br> has to be added to view line breaks in a browser? Why is this not mentioned your notes for the above? My assumption is that \n should show a new line as indicated when testing - however this only shows when showing the source in the browser.

sudhashen@gmail.com - 29 Aug 2008

Great work.Thanks for making this resource available.Please could you give a newbie explanation as to why <br> has to be added to view line breaks in a browser? Why is this not mentioned your notes for the above? My assumption is that \n should show a new line as indicated when testing - however this only shows when showing the source in the browser.

Safiya Dogara Bashir/sdbashir2001@yahoo.com - 29 Aug 2008

Great. That explains the problem I had a while back when I was trying to output escape sequences within single quotes. This book is definitely getting bookmarked! :)

Meelen - 29 Aug 2008

\n is not worling but for me <BR /> is.

What are the other signs?

zhang2000 - 29 Aug 2008

About "...escape sequences only work in double-quoted strings", I test the following on my local site(Apache2.0 + PHP 5.1.1+Win2000), which makes me a little confusing, I mean, sometimes, it works.

$MySingleString = 'This \'will\' work';
print $MySingleString;

The output in the browser like this:
This 'will' work

Tom - 29 Aug 2008

> Windows paths can be expressed with forwards slashes in most cases, the main exception is when building a command-line for some external program call where backslashes are uusually needed.

When offline, they should be using backslashes. It's only when using the file:// protocol that they're forward.

A PHP User - 29 Aug 2008

what happens if we type for variable
$whatHappens= "c:///";

Jasen Betts - 29 Aug 2008

Windows paths can be expressed with forwards slashes in most cases, the main exception is when building a command-line for some external program call where backslashes are uusually needed.

A PHP Newbie - 29 Aug 2008

Great. That explains the problem I had a while back when I was trying to output escape sequences within single quotes. This book is definitely getting bookmarked! :)

hullk - 29 Aug 2008

awesome book I love it



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is zero plus six?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow