1. Computing

Learn PHP - A Beginner's Guide to PHP Programing

By , About.com Guide

3 of 9

PRINT and ECHO Statements
First we are going to learn about the echo statement, the most basic statement in PHP. What this does is output whatever you tell it to echo. For example:

<?php echo "I like About" ?>

This would return the statement I like About. Notice when we echo a statement, it is contained within quotation marks [“”].

Another way to do this is to use the print function. An example of that would be:

<?php print "I like About" ?>

There is a lot of debate about which is better to use or if there is any difference at all. Apparently in very large programs that are simply outputting text the ECHO statement will run slightly faster, but for the purposes of a beginner they are interchangeable.

Another thing to keep in mind is that all of your print/echoing is contained between quotation marks. If you want to use a quotation mark inside of the code, you must use a backslash:

 <?php print "Billy said \"I like About too\"" ?>
When you are using more than one line of code inside your php tags, you must separate each line with a semicolon [;]. Below is an example of printing multiple lines of PHP, right inside your HTML:

 <html>
 <head>
 <title>PHP Test Page</title>
 </head>
 <body>
 <center><b>
 <?php 
 print "I like About";
 print "<br>";
 print "Billy said \"I like About too\"" 
 ?>
 </b></center>
 </body>
 </html>

As you can see, you can insert HTML right into your php print line. You can format the HTML in the rest of the document as you please, but remember to save it as a .php file.

Do you use PRINT or ECHO? Share your answer!

Related Video
Basic PHP Syntax
  1. About.com
  2. Computing
  3. PHP / MySQL
  4. PHP Functions
  5. Learn PHP - PHP Tutorial - Print Echo

©2013 About.com. All rights reserved.