What are Escape Sequences? - Images: Japanese
Hey there, everyone! Good morning!
Continuing from last time, let's take a look at the 'escape characters' that kept popping up in the strings section. This isn't specific to PHP — it's a concept built into all sorts of programming languages. A handy little thing, really.
So, "what exactly is an escape character?" you ask. An escape character is a way to represent characters or symbols that can't be expressed directly in a regular string — using a special sequence of characters instead.
Here's a concrete example. Think about the 'line feed' (the newline character). To insert a newline, you can just press the Enter key. Something like this:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>PHP Test</title> </head> <body> <?php echo "Hello World"; ?> </body> </html>
When this is processed, you get the following output:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>PHP Test</title> </head> <body> Hello World</body> </html>
Yep, the newline is right there. But take a closer look at the PHP part:
<?php echo "Hello World"; ?>
The string itself spans multiple lines. It works, but a single logical operation is now split across multiple lines of code, which can feel a bit messy. On top of that, in some other languages, breaking a string across lines like this is actually not allowed — so it's generally not a great habit to get into.
If you're bothered by that, try using an escape character instead. You may recall '\n' from the previous article — that's the newline character. The code above can be rewritten like this. Note: make sure you use '"' (double quotes), not ''' (single quotes). With single quotes, '\n' will be output literally instead of as a newline.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>PHP Test</title> </head> <body> <?php echo "Hello\nWorld"; ?> </body> </html>
Let's run that and see what we get:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>PHP Test</title> </head> <body> Hello World</body> </html>
Same result as before. When you need a newline in your output, using an escape character is the clean way to do it.
If you want an HTML line break instead, you can just output a <br> tag directly. Like this:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>PHP Test</title> </head> <body> <?php echo "Hello<br>World"; ?> </body> </html>
This is also a commonly used technique, so it's worth remembering.
Escape characters cover more than just newlines — there are tab characters and many others too. If you ever need a special character, searching online is a good move. Here's a link to the official PHP reference page for escape sequences:
http://php.net/manual/en/regexp.reference.escape.php
If you're ever unsure whether something is a special character, a good rule of thumb is: if it's not listed on that page, it won't be treated as one.
Now, a quick note about something that trips people up. The '\' (backslash) character is commonly used in escape sequences — and entering it can be a little tricky depending on your OS.
On macOS, you can type it directly with Option + ¥ (on a JIS keyboard). Windows users, however, have a slightly more confusing situation.
On Windows, pressing the '¥' key inputs a backslash — but depending on the font, it displays as the yen symbol '¥' instead of '\'. In other words, what looks like '¥' on your screen is actually a backslash under the hood. If you're on Windows and wondering why everything looks like a yen sign, that's what's going on. It's a bit of a quirk you just have to get used to, unfortunately.
Alright, with that out of the way, let's look at another use of the 'escape sequence' (backslash). One of its most useful tricks is that it can 'escape' the character that follows it.
Let's revisit something from the string syntax we covered earlier. In PHP (and most languages), you can't include a ''' inside a single-quoted string, and you can't include a '"' inside a double-quoted string. This comes up a lot in PHP since you often need to output HTML — and HTML attributes use double quotes all the time.
For example, take the string '<p class="hoge">This is a p element with the class name hoge!</p>'. This is a perfectly normal HTML element, but notice it contains '"' characters. If you try to wrap this entire thing in double quotes, PHP can't tell where the string begins and ends — it'll see an unmatched quote and throw an error.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>PHP Test</title> </head> <body> <?php echo "<p class="hoge">This is a p element with the class name hoge!</p>"; ?> </body> </html>
The code above won't work. The page will come out blank.
The quickest fix is to switch from double quotes to single quotes as the string delimiter.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>PHP Test</title> </head> <body> <?php echo '<p class="hoge">This is a p element with the class name hoge!</p>'; ?> </body> </html>
That works perfectly. When your string contains ", just wrap the whole thing in ' instead. This is a go-to technique — keep it in your back pocket. It works in other languages too.
Alternatively, you can solve the same problem using an escape sequence. Take a look at this:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>PHP Test</title> </head> <body> <?php echo "<p class=\"hoge\">This is a p element with the class name hoge!</p>"; ?> </body> </html>
See the '\' right before each '"'? Writing '\"' escapes the double quote — it strips away its special meaning as a string delimiter and treats it as just a plain character. That way you can safely include '"' inside a double-quoted string. For single quotes, '\'' does the same thing. This is one of the most useful things about escape sequences, so make sure it sticks. And if you ever need to output an actual backslash, just write '\\' and you're good.
So that's the rundown on escape characters and escape sequences. In the next article, we'll look at various ways to output strings and other values in PHP. See you there!
That's all for now — take care, and see you next time!
This article was written by Sakurama.
Author's beloved small mammal |
桜舞 春人 Sakurama HarutoA Tokyo-based programmer who has been creating various content since the ISDN era, with a bit of concern about his hair. A true long sleeper who generally feels unwell without at least 10 hours of sleep. His dream is to live a life where he can sleep as much as he wants. Loves games, sports, and music. Please share some hair with him. |
If you find any errors or copyright issues, please contact us.