PHP: Escape sequences
Let's say we want to show a dialog between the Mother of dragons with his child:
- Are you hungry? - Aaaarrrgh!
If we print this text to the screen:
print_r("- Are you hungry?- Aaaarrrgh!");
we'll get the following output:
- Are you hungry?- Aaaarrrgh!
We must somehow communicate the action "to press enter" to the interpreter to start a new line after the question mark.
In PHP the line break is specified by '\n':
print_r("- Are you hungry?\n- Aaaarrrgh!");
- Are you hungry? - Aaaarrrgh!
'\n' is an example of escape sequences. They are also called control structures. Such control structures are not visible in the result string, as they are not displayed the way they were typed in.
When you type a text in an editor like Word and press Enter at the end of each line, the editor inserts there a special invisible character called LINE FEED (LF). In some editors you can even turn on the display of such non-printable characters. It will look like this:
- Hi!¶ - Oh, hi!¶ - How are you?¶
Device that will output this text will react to that symbol. When printers encounter the LF symbol, they advance the paper by one line, and editors move all the subsequent text down one line.
There are dozens of such symbols, but in programming, however, you will deal with few of them. Apart from line break, such sequences include tab space '\t' and carriage return '\r\' (only in Windows). A controlling structure can be easily recognized by the backlash '\'. We, programmers, often use the line break sequence '\n' for correct display of the text.
<?php print_r("Gregor Clegane\nDunsen\nPolliver\nChiswyck");
Attention! Escape sequences like '\n' only work when enclosed in double quotes.
We'll have the following output:
Gregor Clegane Dunsen Polliver Chiswyck
A couple of details to pay attention to:
1. It doesn't matter if the '\n' sequence is followed by a character or an empty string. The line break will be recognized and executed in any case.
2. Do you remember that a string can contain just one character or no characters at all? A string can also contain just '\n':
print_r('Gregor Clegane'); print_r("\n"); print_r('Dunsen');
In the code above we print a string with a name, another string with a line break and one more string. The program will output the following:
Gregor Clegane Dunsen
3. A sequence like '\n' may look like two separate characters in the original text, but the interpreter sees it as one special symbol.
\4. If we need to get '\n' displayed as a text (two separate characters), we can escape it with another backlash '\' before it. That means that the sequence '\n' will be displayed as character '\' followed by 'n'.
print_r("Joffrey loves using \\n");
Joffrey loves using \n
A small but important note on Windows. In Windows by default the sequence used to create a line break is '\r\n'. Such combination works perfectly in Windows, but creates problems when transferred to other systems (e.g. when developers in one team use both Windows and Linux). It happens that the sequence '\r\n' is interpreted differently depending on the chosen charset (we will see it later). This is why it is a common practice among developers to always use '\n' without '\r', because LF is interpreted in the same way and works in any system. Don't forget to change your editor line break settings to '\n'.
Write a program that prints the following to the screen:
- Did Joffrey agree? - He did. He also said "I love using \n".
The program must have just one 'print_r', and the result must look exactly the same like the text above.
Escape Sequence - a special combination of characters in the text.
\n, for example, specifies a line break