PHP Echo and Print Statements


In this tutorial, you will learn how to use the PHP echo() and print() statements to show the output in a web browser.


PHP echo() and print() Statements

Echo() and print() statements are greater or less the equal. they’re each used to output data to the screen.

The differences are echo and print:

The echo function is significant because it does not return anything, but print statement will return 1 when the statement is successfully output. echo statement can take multiple parameters while print statement can take one argument. that’s why echo statement is faster than print statement.

The PHP echo Statement

The Echo statement can detect one or more strings. on normal terms, echo statements can display something that can appear in the browser, such as strings, numbers, variables value, expression results, etc.

Since echo is a language that is not actually a function (such as statement), you can use brackets without using it e.g. echo or echo(). However, if you want to pass multiple parameters for echo, then parentheses should not be in brackets.

Display Strings of Text

The following example will show how to output a string of text using echo statements:

Code Example:
<?php
   // Displaying string of text
   echo "Hello Coder Well !";
?>

This returns the following:

Hello Code Grope!

Display Variables

The following example will show how to output variables with the echo statement:

Code Example:
<?php
   $text1 = "CoderWell";
   $text2 = "coderwell.com";
   $x = 10;
   $y = 5;
   $xy = $x + $y;

   echo "<h2>" . $text1 . "</h2>";
   echo " Learn PHP with the " . $text2 . "<br>";
   echo $xy;
?>

This returns the following:

CoderWell

Learn PHP with the coderwell.com
15

Display HTML Code

The following example will show how to output HTML code with the echo statement:

Code Example:
<?php
   // Displaying HTML code
   echo "<h5>Code Grope Easy Web Programming. </h5>";
   echo "<h5 style='color: blue;'>Code Grope Easy Web Programming with style. </h5>";
?>

This returns the following:

Code Grope Easy Web Programming.
Code Grope Easy Web Programming with style.

The PHP print Statement

you may also use the print statement (an alternative to echo) to show output to the browser. so you also can be used with or without parentheses like: print or print().

Display Strings of Text

The following example will show how to output a string of text using print statements:

Code Example:
<?php
   // Displaying string of text using print
   print "Hello Coderwell!";
?>

This returns the following:

Hello Coderwell!

Display Variables

The following example will show how to output variables with the print statement:

Code Example:
<?php
   $text1 = "CoderWell";
   $text2 = "coderwell.com";
   $x = 10;
   $y = 5;
   $xy = $x + $y;

   print "<h2> $text1 </h2>";
   print "Learn PHP with the $text2 <br>";
   print $xy;
?>

This returns the following:

CoderWell

Learn PHP with the coderwell.com
15

Display HTML Code

The following example will show how to output HTML code with the print statement:

Code Example:
<?php
   // Displaying HTML code using print
   print "<h5>Code Grope Easy Web Programming. </h5>";
   print "<h5 style='color: blue;'>Code Grope Easy Web Programming with style. </h5>";
?>

This returns the following:

Code Grope Easy Web Programming.
Code Grope Easy Web Programming with style.