English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

PHP Reference Manual

PHP echo & print

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

PHP echo statement

The echo statement can output one or more strings. Generally, the echo statement can display any content that can be displayed to the browser, such as strings, numbers, variable values, and the results of expressions, etc.

Since echo is a language construct, not an actual function (likeifconstruct, so it can be used without parentheses, such as echo or echo(). However, if you need to pass multiple parameters to echo, you cannot enclose these parameters in parentheses.

Display text string

The following example will show you how to use the echo statement to display a text string:

<?php
//Display text string
echo "Hello World!";
?>
Test and see‹/›

The output of the above PHP code is as follows:

Hello, World!

Display HTML code

The following example will show you how to use the echo statement to display HTML code:

<?php
//Display HTML code
echo "<h4">This is a simple title.</h4">
echo "<h4 style='color: red;'>This is a styled title.</h4">
?>
Test and see‹/›

The output of the above PHP code is as follows:

This is a simple title.

This is a styled title.

Display variables

The following example will show you how to use the echo statement to display variables:

<?php
//Define variables
$txt = "Hello World!";
$num = 123456789;
$colors = array("Red", "Green", "Blue");
 
//Define variables
echo $txt;
echo "<br>";
echo $num;
echo "<br>";
echo $colors[0];
?>
Test and see‹/›

The output of the above PHP code is as follows:

Hello, World!
123456789                    
red

PHP print statement

You can also use the print statement (an alternative to echo) to display output to the browser. Like echo, printing is also a language construct, not a real function. Therefore, you can also use it without parentheses: print or print().

The echo and print statements work in exactly the same way, but the print statement can only output a string and always returns1. This is why the echo statement is considered to be a bit faster than the print statement because it does not return any value.

Display text string

The following example will show you how to use the print statement to display a text string:

<?php
//Display text string
print "Hello World!";
?>
Test and see‹/›

The output of the above PHP code is as follows:

Hello, World!

Display HTML code

The following example will show you how to use the print statement to display HTML code:

<?php
//Display HTML code
print "<h4">This is a simple title.</h4">
print "<h4 style='color: red;'>This is a styled title.</h4">
?>
Test and see‹/›

The output of the above PHP code is as follows:

This is a simple title.

This is a styled title.

Display variables

The following example will show you how to use the print statement to display variables:

<?php
//Define variables
$txt = "Hello World!";
$num = 123456789;
$colors = array("Red", "Green", "Blue");
 
//Define variables
print $txt;
print "<br>";
print $num;
print "<br>";
print $colors[0];
?>
Test and see‹/›

The output of the above PHP code is as follows:

Hello, World!
123456789                    
red