Introduction to PHP - Displaying Using the print and echo Statements

Guest Posts Paolo Nikko Nuñal

In the previous tutorial, we discussed about PHP variables, in this tutorial we are going to talk about printing values using the echo and print functions. This is the most common way in printing output statements.

So you might ask, what is the difference between the echo and print statements. The answer is simple, the echo statement can print more than one strings, while the print can output only one string and return always 1.

Code:

<html>
  <head>
     <title>My Title</title>
  </head>
  <body>
      <?php
          echo "Hello World";
          echo "This is a string printed in echo";
          echo "<h2>H2 header printed in PHP </h2>";


      <?
  </body>
</html>


Displaying Variables with the echo statement

Code:

<html>
  <head>
     <title>My Title</title>
  </head>
  <body>
      <?php
          $name = "John";
          $age  = 23;
          $address = "United States";
          echo "My name is".$name;
          echo "I'm".$age."years old";
          echo "I lived in the".$address;


      <?
  </body>
</html>


The PHP print statement.

Code:

<html>
  <head>
     <title>My Title</title>
  </head>
  <body>
      <?php

          print "My Name is John";
          print "I'm 23 years old";
          print "Hello PHP";


      <?
  </body>
</html>


Displaying Variables with the print statement

Code:

<html>
  <head>
     <title>My Title</title>
  </head>
  <body>


     <?php
          $name = "John";
          $age  = 23;
          $address = "United States";
          print "My name is".$name;
          print "<br>";
          print "I'm".$age."years old";
          print "<br>";
          print "I lived in the".$address;


      <?


  </body>
</html>


That's it, you can now print using the echo and print statements.