PHP Echo And Print Tutorial With Examples

Objective

What is PHP ECHO and PRINT
How to use Echo and Print in PHP script

PHP Echo and Print, both are used for displaying text, string and variables value to browser. It is very easy to use it in PHP programming. You can use PHP Echo and Print like this.

<?php
echo "Hello Move2Code.com";
print "Hello Move2Code.com";
?>

Complete Programming Example

<!DOCTYPE html>
<html>
<head>
    <title>PHP Echo and Print</title>
</head>
<body>
    <h1>PHP Echo and Print</h1>
	<?php
      $str="Hello world";
      $num=239490;
      $Days=Array("sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
      
      //Echo
      echo '<h3 style="color:green">Echo Example</h3>';
      echo '<h3>'.$str.'</h3>';
      echo $num;
      echo "<br>";
      echo $Days[3];
      echo "<br>----------------------------------";
      
      //Print
      print '<h3 style="color:green">Print Example</h3>';
      print '<h4>'.$str.'</h4>';
      print $num;
      print "<br>";
      print $Days[3];
      print "<br>-----------------------------------";
    ?>
</body>
</html>

Output

Echo Example

Hello world

239490
Wednesday
———————————-

Print Example

Hello world

239490
Wednesday
———————————–

More fact about Echo and Print

ECHO STATEMENT

  1. Echo and Print, both are PHP Statement.
  2. Both are used for displaying text, string or variables values on the browser.
  3. Echo statement can be used with or without parenthesis(). For Example, echo "Hello World" and echo ("Hello World") are same.
  4. Echo can pass multiple strings with comma separator. For Example echo ("This is ",$str," How are you"). It will print This is Hello World. How are you.
  5. Echo is faster than Print Statement.
  6. You can use dot (.) or comma (,) as a joiner to join string.
Echo Example
<!DOCTYPE html>
<html>
<head>
    <title>PHP Echo and Print</title>
</head>
<body>
    <h1>PHP Echo and Print</h1>
	<?php
      $str="Hello world";
      $num=239490;
      $Days=Array("sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
      
      //Echo
      echo ('<h3 style="color:green">Echo Example</h3>');
      echo $str," ",$num," ",$Days[3];
    ?>
</body>
</html>
Output

Echo Example

Hello world
239490
Wednesday

PRINT STATEMENT


  1. Print also can be used with or without parenthesis. For example, print "Hello World" and print("Hello world") are same.
  2. Print cannot pass multiple arguments.
  3. Print statement behaves like a function and always return 1. So, it is quite slower than Echo.
  4. Print statement can be used in more complex programming structure.
  5. You can use dot (.) or comma (,) as a joiner for join string.
<!DOCTYPE html>
<html>
<head>
    <title>PHP Echo and Print</title>
</head>
<body>
    <h1>PHP Echo and Print</h1>
	<?php
      $str="Hello world";
      $num=239490;
      $Days=Array("sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
      
      //Echo
      print ('<h3 style="color:green">Print Example</h3>');
      $result = print $str." ".$num." ".$Days[3];
      print "<br>Result = ".$result;
    ?>
</body>
</html>

Print Example

Hello world
239490
Wednesday
Result = 1

SUMMARY

In this tutorial you learn how to use PHP Echo and Print. You also learned the basic difference between Echo and Print in PHP. In the next chapter you will learn PHP Variables and Datatypes.