PHP: Escape equences
We want to show a dialogue between the Mother of Dragons and her child:
- Are you hungry? - Aaaarrrgh!
If you display a line with this text on the screen:
<?php print_r("- Are you hungry?- Aaaarrrgh!");
it goes like this:
- Are you hungry?- Aaaarrrgh!
Not what we wanted. The lines are one after the other, not one below the other. We need to somehow tell the interpreter to "hit enter", i.e., to go to the next line after the question mark. This can be done by using the line feed character:
<?php print_r("- Are you hungry?\n- Aaaarrrgh!");
- Are you hungry? - Aaaarrrgh!
\n is a special symbol. In the literature, it's often referred to as the LF (Line Feed). You may have thought now that it was a typo because here we can see two characters,
n, but that's not the case. From the computer's perspective, this is one invisible line feed character. Proof:
<?php // We didn't study it, but you should know the truth // Below is some code that returns the length of a string strlen("a"); // 1 strlen("\n"); // 1 !!! strlen("\n\n"); // 2 !!!
Why is this done?
\n is just a way to write the line feed character, but the line feed itself is really just one character, it's simply invisible. That's why this challenge arose. It had to be represented somehow on the keyboard. And since the number of characters on the keyboard is limited and given to the most important, all special characters are implemented in the form of these signs.
The line feed character isn't something specific to programming. Anyone who has ever typed on a computer has used the line feed by hitting Enter. Many editors have an option to enable the display of invisible characters; you can use it to see where they are (although it's only displayed a schematic display, these characters would otherwise have no graphical representation, they're invisible):
- Hey!¶ - Oh, hey!¶ - How's it going?
The device that outputs the corresponding text takes this character into account. For example, the printer pulls the paper up one line when it encounters the LF, and the text editor pulls all subsequent text below, also by one line.
\n is an example of an escape sequence. They're also called control constructs. Although there are more than a dozen such characters, there are often only a few such characters in programming. In addition to line feeds, these can include tabs (the break you get when you press the Tab button) and carriage returns (only in Windows). We programmers often need to use line feed
\n for proper text formatting.
<?php print_r("Gregor Clegane\nDunsen\nPolliver\nChiswyck");
Warning! Escape sequences like
\n only work inside double quotes!
The screen will display:
Gregor Clegane Dunsen Polliver Chiswyck
Note the following points:
It does not matter what comes before or after
\n: a character or an empty string. The line feed will be spotted and executed in any case.
Remember that a string can contain a single character or zero characters. Also, a string can only contain
\n. Analyze the following example:
<?php print_r('Gregor Clegane'); print_r("\n"); print_r('Dunsen');
Here we output one string with a name, then one string with a line feed and then another string. The program will display this on the screen:
Gregor Clegane Dunsen
If we need to output
\nas text (two separate printable characters), we can use the escape method we already know, adding another
\at the beginning. I.e., the sequence
\\nwill appear as the characters
nfollowing each other.
<?php print_r("Joffrey loves using \\n");
on the screen, you'll see:
Joffrey loves using \n
A small but important note about Windows. Windows uses
\r\n. to do line feeds by default. This combination works well only in Windows, but creates problems when migrating to other systems (for example, when the development team has both Windows and Linux users). The point is that the sequence
\r\n has a different interpretation depending on the chosen encoding (discussed later). For this reason, it's common among developers to always use
\r, since LF is always treated the same and works fine on any system. Remember to configure your editor to use
Write a program that prints this to the screen:
- Did Joffrey agree? - He did. He also said "I love using \n".
This program uses only one
print_r(), but the result on the screen should look exactly like the one shown above.
The exercise doesn't pass checking. What to do? 😶
If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:
- Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
In my environment the code works, but not here 🤨
Tests are designed so that they test the solution in different ways and against different data. Often the solution works with one kind of input data but doesn't work with others. Check the «Tests» tab to figure this out, you can find hints at the error output.
My code is different from the teacher's one 🤔
It's fine. 🙆 One task in programming can be solved in many different ways. If your code passed all tests, it complies with the task conditions.
In some rare cases, the solution may be adjusted to the tests, but this can be seen immediately.
I've read the lessons but nothing is clear 🙄
It's hard to make educational materials that will suit everyone. We do our best but there is always something to improve. If you see a material that is not clear to you, describe the problem in “Discussions”. It will be great if you'll write unclear points in the question form. Usually, we need a few days for corrections.
By the way, you can participate in courses improvement. There is a link below to the lessons course code which you can edit right in your browser.
Be sure to experiment with the output of different lines on the site https://repl.it/languages/php
An escape sequence is a special combination of characters in text. For example,
\nmeans line feed.