PHP 5 echo and print statements


In PHP, there are two basic ways to output: echo and print.

In this chapter, we will discuss the usage of the two statements in detail and demonstrate how to use echo and print in examples.


PHP echo and print statements

The difference between echo and print:

  • echo - can output one or more strings
  • print - only allows outputting one string, and the return value is always 1

Tip:echo outputs faster than print. echo has no return value, while print has a return value of 1.


PHP echo statement

echo is a language construct. When using it, you can either omit the parentheses or add them: echo or echo().

Display strings

The following example demonstrates how to use the echo command to output strings (strings can contain HTML tags):

Example

<?php echo "<h2>PHP is very interesting!</h2>"; echo "Hello world!<br>"; echo "I want to learn PHP!<br>"; echo "This is a", "The string,", "uses", "multiple", "parameters."; ?>

Try it Yourself »

Display variables

The following example demonstrates how to use the echo command to output variables and strings:

Example

<?php $txt1="Learn PHP"; $txt2="example.com"; $cars=array("Volvo","BMW","Toyota"); echo $txt1; echo "<br>"; echo "In$txt2Learn PHP"; echo "<br>"; echo "My car's brand is{$cars[0]}"; ?>

Try it Yourself »


PHP print statement

print is also a language construct. You can use parentheses or not: print or print().

Display strings

The following example demonstrates how to use the print command to output strings (strings can contain HTML tags):

Example

<?php print "<h2>PHP is very interesting!</h2>"; print "Hello world!<br>"; print "I want to learn PHP!"; ?>

Try it Yourself »

Display variables

The following example demonstrates how to use the print command to output variables and strings:

Example

<?php $txt1="Learn PHP"; $txt2="example.com"; $cars=array("Volvo","BMW","Toyota"); print $txt1; print "<br>"; print "In$txt2Learn PHP"; print "<br>"; print "My car's brand is{$cars[0]}"; ?>

Try it Yourself »
Other Extensions