With PHP, there are two basic ways to get output: echo and print.
In this tutorial we use echo or print in almost every example. So, this chapter contains a little more info about those two output statements.
PHP echo and print Statements
echo and print are more or less the same. They are both used to output data to the screen.
The differences are small: echo has no return value while print has a return value of 1 so it can be used in expressions. echo can take multiple parameters (although such usage is rare) while print can take one argument. echo is marginally faster than print.
The PHP echo Statement
The echo statement can be used with or without parentheses: echo or echo().
Display Text
The following example shows how to output text with the echo command (notice that the text can contain HTML markup):
Example
<?php
echo "<h2>PHP is Fun!</h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This ", "string ", "was ", "made ", "with multiple parameters.";
?>
Try it Yourself »
Display Variables
The following example shows how to output text and variables with the echo statement:
Example
<?php
$txt1 = "Learn PHP";
$txt2 = "W3Schools.com";
$x = 5;
$y = 4;
echo "<h2>" . $txt1 . "</h2>";
echo "Study PHP at " . $txt2 . "<br>";
echo $x + $y;
?>
Try it Yourself »
The PHP print Statement
The print statement can be used with or without parentheses: print or print().
Display Text
The following example shows how to output text with the print command (notice that the text can contain HTML markup):
Example
<?php
print "<h2>PHP is Fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
?>
Try it Yourself »
Display Variables
The following example shows how to output text and variables with the print statement:
Example
<?php
$txt1 = "Learn PHP";
$txt2 = "W3Schools.com";
$x = 5;
$y = 4;
print "<h2>" . $txt1 . "</h2>";
print "Study PHP at " . $txt2 . "<br>";
print $x + $y;
?>
Try it Yourself »
❮ PHP String Reference
Example
Write some text to the output:
<?php
echo "Hello world!";
?>
Try it Yourself »
Definition and Usage
The echo() function outputs one or more strings.
Note: The echo() function is not actually a function, so you are not required to use parentheses with it. However, if you want to pass more than one parameter to echo(), using parentheses will generate a parse error.
Tip: The echo() function is slightly faster than print().
Tip: The echo() function also has a shortcut syntax. Prior to PHP 5.4.0, this syntax only works with the short_open_tag configuration setting enabled.
Syntax
Parameter Values
strings | Required. One or more strings to be sent to the output |
Technical Details
No value is returned |
4+ |
More Examples
Example
Write the value of the string variable ($str) to the output:
<?php
$str = "Hello world!";
echo $str;
?>
Try it Yourself »
Example
Write the value of the string variable ($str) to the output, including HTML tags:
<?php
$str = "Hello world!";
echo $str;
echo
"<br>What a nice day!";
?>
Try it Yourself »
Example
Join two string variables together:
<?php
$str1="Hello world!";
$str2="What a nice day!";
echo $str1 . " " . $str2;
?>
Try it Yourself »
Example
Write the value of an array to the output:
<?php
$age=array("Peter"=>"35");
echo "Peter is " . $age['Peter'] . " years old.";
?>
Try it Yourself »
Example
Write some text to the output:
<?php
echo "This text
spans multiple
lines.";
?>
Try it Yourself »
Example
How to use multiple parameters:
<?php
echo 'This ','string ','was ','made ','with multiple parameters.';
?>
Try it Yourself »
Example
Difference of single and double quotes. Single quotes will print the variable name, not the value:
<?php
$color = "red";
echo "Roses are $color";
echo "<br>";
echo 'Roses are $color';
?>
Try it Yourself »
Example
Shortcut syntax (will only work with the short_open_tag configuration setting enabled):
<?php
$color = "red";
?>
<p>Roses are <?=$color?></p>
Try it Yourself »
❮ PHP String Reference