Escape Sequence Indicators in PHP

Escape sequence indicators in php

When programming in PHP it's sometimes necessary to treat characters differently in order to perform a particular task. One common coding method is concatenating variables into echo strings. that is where you have a string and inside there are one or more PHP variables. In order for PHP not to echo out the dollar and variable literally, you can use escape sequence indicators in PHP. Let's explore what they are, including the different types, and how to use them with code examples along the way.

What are Escape Sequence Indicators in PHP?

Escape Sequence Indicators are a special method of telling PHP that you want to "escape" a particular section of the code. That's typically done with the backslash character "\" and PHP including your IDE, will know that the following character should be escaped. The term escaped is the word used to describe how PHP handles the character. You escape a character when you don't want it to behave in the normal way and therefore get treated differently. Let's explore an example with PHP code.

Escaping Quotes in a String PHP

One of the most common examples of escaping in PHP is escaping quote marks in an echo string in PHP. To echo a string out to the screen, you must wrap the words in quotes. Either single or double works here, but in order, for variables in strings to work correctly, you'll need double quotes. Below you will notice that the variable "$location" has a backslash before the single quote on "o\'". This backslash character tells PHP that we don't want to close the string and instead want to escape the character. By doing this it means we instead include the single quote in our string. If we were to var_dump the variable $location its contents are; John o' Groats.

$location = 'John o\' Groats';
echo "The most northern point of the UK is $location";

# Outputs
The most northern point of the UK is John o' Groats

It is the same for double quotes too and works the same way by using that magic backslash escape character. Let's see double quotes in action. Similar to the above, but instead of escaping a single quote (') you escape the double quote (").

$sizeInInches = "3,800\"";
echo "Big Ben in London is $sizeInInches tall";

# Outputs
Big Ben in London is 3,800" tall

It's worth noting that you don't need to escape quotes if the string is wrapped with the other. See the string below, the word "don't" does not need to be escaped because the string is wrapped with double quotes, not single quotes

$foo = "You don't need to escape anything here";

Escaping backslashes in PHP

One that might not be obvious, but it's also possible to escape the escape character! It goes further than that, you can even escape the escaped character! Let's explore the below example. Here we're attempting to echo a string "Sometimes is it 50\50". But because we've used an escape character, the backslash, the output won't be what we want. The backslash will escape and the next set of characters, in this case, "\50" and convert it into an octal value from the octal escape sequence. In this case "(".

echo "Sometimes is it 50\50";

# Outputs
Sometimes it is 50(

To overcome this issue, we must escape the escape character, like the below PHP code.

echo "Sometimes is it 50\\50";

# Outputs
Sometimes it is 50\50

It can even get out of hand when escaping a character like the one below. We have to add 10 (!) backslashes to ensure each backslash is escaped property including not escaping the end quote.

echo "This is an example of five backslashes: \\\\\\\\\\";

# Outputs
This is an example of five backslashes: \\\\\

Escaping a dollar symbol in PHP

We know we're working in PHP when we see variables with dollar symbols, and because of this if we include a dollar symbol in our string, we'll need to ensure we escape this character too, otherwise, we will get output that is not desired. Let's say we had a shopping basket and at the bottom the price of its contents. It might be that our business model is price on request, and instead of showing a price we might put "TBC" (to be confirmed). The below example not only outputs an incomplete string but also triggers a PHP warning, an undefined variable.

echo "Your shopping basket price is: $TBC";

# Triggers
Warning: Undefined variable $TBC in

# Outputs
Your shopping basket price is:

To fix the output we need to ensure we've escaped the dollar symbol, like in the example below. Now, PHP isn't expecting there to be a variable "TBC", and will instead output it literally.

echo "Your shopping basket price is: \$TBC";

# Outputs
Your shopping basket price is: TBC

Common Escaping Characters Used in PHP

There are many different types of escape characters that can be used in PHP for various things that go beyond escaping quotes, dollar symbols, and the escape characters themselves. Let's explore more common escaping characters used in PHP code.

Newline character (\n) in PHP

On Linux-based machines, to split a string onto a new line, use the backslash "n" (\n) escape charter.

echo "In Linux, the next word will be \n on a new line";
# Outputs
In Linux, the next word will be 
 on a new line

Return character (\r) in PHP

On Windows-based machines, to split a string onto a new line, use the backslash "r" (\r) known as the carriage return charter.

echo "In Windows, the next word will be \r on a new line";
# Outputs
In Windows, the next word will be 
 on a new line

Hexadecimal characters in PHP

It's possible to output a hexadecimal character using backslash x followed by two (or four) hexadecimal value sequence numbers (\x<HH>). In the below example, we're outputting the number 123, by using the hexadecimal values which are 31, 32, and 33.

echo "\x31\x32\x33";

# Outputs;
123

Tab character in PHP

To output a tab character you can use the backslash "t" (\t). When echoed to the screen you'll notice that the text doesn't start at the beginning of the new line, and instead as predicted tabbed once.

echo "\t this text is tabbed.";

# Outputs;
 this text is tabbed.

Conclusion

Escape sequence indicators in PHP are a must for any developer. They allow you to perform tasks such as outputting literal reserver words in strings, including outputting tabs, newlines, and hexadecimal characters. Remember that the sequence indicators are interpreted by PHP with a backslash character.

Senior PHP developer with near two decades of PHP experience. Author of Dev Lateral guides and tools. The complete place for PHP programmers. Available to hire to help you build or maintain your PHP application.

Looking for industry-leading PHP web development?

API development WordPress Hosting ★ and more 🐘

We use cookies to enhance your browsing experience and analyse website traffic in accordance with our Privacy and Cookie Policy. Our cookies, including those provided by third parties, collect anonymous information about website usage and may be used for targeted advertising purposes. By clicking "Reject non-essential" you can opt out of non-essential cookies. By clicking "Accept all" you agree to the use of all cookies.


Reject non-essential Accept all