PHP echo and PHP print are both output statements used to print the output to the screen.

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:

echo "This text extends to multiple lines, the new lines are 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:

C:\localhost\php_lesson1\lessons>php echo.php This text extends to multiple lines, the new lines are output as well.

Another way of writing line breaks to command prompt is to use the "\n" character:

echo "This text \nextends to multiple lines,\nthe new lines are output as well.";

Which will produce exactly the same result as previous example:

C:\localhost\php_lesson1\lessons>php echo.php This text extends to multiple lines, the new lines are output as well.

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 = "This is some other random\r\n text that will be printed on different lines\r\n when output in a text file." //code to write our output in a new text file called newfile.txt $new_file = fopen( "newfile.txt", "w" ) or die( "Unable to open file!" ); fwrite( $new_file, $output ); fclose( $new_file );

Output inside the newfile.txt:

This is some other random text that will be printed on different lines when output in a text file.

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:

<html> <body> <?php echo "This is some other random<br /> text that will be printed on different lines<br /> when output in a browser."; ?> </body> </html>

Output in a browser:

This is some other random text that will be printed on different lines when output in a browser.

Interpolation

This fancy sounding word refers to the use of variables inside of quotes in PHP:

$cost = 20; //variable is expanded inside double quotes echo "This product costs $cost"; //output: This product costs 20 //variable is not expanded inside single quotes echo 'This product costs $cost'; //output This product costs $cost

Arrays can also be printed inside double quotes, though we have to surround it with curly brackets

$animal = array( 'sound' => 'meow' ); echo "Cats {$animal[ 'sound' ]} when they're hungry"; //output: Cats meow when they're hungry