PHP echo and print
PHP echo and PHP print are both output statements used to print the output to the screen. Remember that both echo and print are language constructs, they can be used with and without parenthesis.
New Line Breaks
I think it's worth clarifying how PHP new lines work from the beginning to avoid a lot of confusion later on. Most of this confusion comes from the context of where these line breaks are used, I'm going to cover three of them: Outputting to command prompt, writing to files, and printing to web browsers.
Command Prompt New Lines ( \n )
When using the echo statement and putting your text on new lines, the new lines will be created in the output as well:
However, what a lot of beginners coders don't realize, is this is only referring to command prompt output, where PHP output is rarely used:
Another way of writing line breaks to command prompt is to use the "\n" character:
Which will produce exactly the same result as previous example:
New Lines When Writing to Files ( \r\n )
Simply using \n won't work when writing our output to files, and even though it doesn't involve the echo or print statement, I thought it would be a good idea to include the example here as well. The proper way to include line breaks when writing text to files is to use "\r\n"
Output inside the newfile.txt:
New Lines When Printing to Browsers (<br/>)
We finally get to the most common use of line breaks, when printing text in a browser, using the <br/> HTML tag. None of the previous examples would work in a browser because the browser interprets text a little bit differently and needs tags for formatting. So we would re-write the previous example as follows:
Output in a browser:
Interpolation
This fancy sounding word refers to the use of variables inside of quotes in PHP:
Arrays can also be printed inside double quotes, though we have to surround it with curly brackets