A simple article that I had saved there for quite some time and that will surely be useful to those of you who are starting with PHP is to know how to set line breaks in PHP. Since surely when you are showing your first results on the console they all appear together and are difficult to read or interpret.
If you are a person who has a medium or advanced knowledge of the PHP programming language you will surely find this example obvious or simply of little use. Although, think that you had a moment when you started to learn how to program with PHP and I’m sure that this doubt has plagued you more than once. So respect those who give their first steps with PHP and help them with it.
Well let’s get to it. The first thing we have to do in our PHP program will be to create a console output using the echo function.
echo "I am a line.";
We could think that by putting the echo function again inside our code what would happen is that it would generate a line break.
echo "I am a line."; echo "I am another line.";
However, what happens is that in the console we will see the two texts in a row.
I am a line. I am another line.
If we want to add line breaks, what we should do is use the n character within the text string. This way, when the compiler encounters the n character, it will automatically generate a line break through the console.
Thus our code in PHP will look like this:
echo "I am a line.n"; echo "I am another line.n";
In this way the console output will be:
I am a line.
I am another line.
The n character can be used anywhere in the text string. It doesn’t just have to go to the end of the chain. That is why we would have the same effect as what we had achieved until now with the following code in PHP:
echo "I am a line of code.nI am another line of code";
With this you now know how you can create line breaks in PHP when you are taking your first steps with this language.
