PHP Echo/Print

Echo and Print Statements in PHP 7

echo and print Statements in PHP 7: Especially, In PHP there are two basic ways to get the output: echo and print. Therefore, In this tutorial we use echo as well as print in almost every example. So, this chapter contains a little bit more info about those two output statements.

PHP echo and print Statements

echo and print more or less the same.Hence, 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. Basically, how to output text with the echo command (notice that the text can contain HTML markup):

<?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.";
?>

Output:

PHP is Fun!

Hello, world! I'm about to learn PHP! This string was made with multiple parameters.

 

Display Variables

The following example shows. Basically, how to output text and variables with the echo statement:

 

<?php
$txt1 = "Learn PHP";
$txt2 = "LearnPHPonline.in";
$x = 5;
$y = 4;

echo "<h2>" . $txt1 . "</h2>";
echo "Study PHP at " . $txt2 . "<br>";
echo $x + $y;
?>

Output:

Learn PHP

Study PHP at LearnPHPonline.in 9

 

The PHP print Statement

The print statement can be used with or without parentheses: print or print().

Display Text and Variable

The following example shows. Basically, how to output text with the print command (notice that the text can contain HTML markup):

 

<?php // text 
print "<h2>PHP is Fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
?>

<?php // variable
$txt1 = "Learn PHP";
$txt2 = "LearnPHPonline.in";
$x = 5;
$y = 4;

print "<h2>" . $txt1 . "</h2>";
print "Study PHP at " . $txt2 . "<br>";
print $x + $y;
?>

 

Output:

PHP is Fun!

Hello world! I'm about to learn PHP!

Learn PHP

Study PHP at LearnPHPonline.in 9

The Difference Between the echo and print Commands

So far, you have seen the echo command used in a number of different ways to output text from the server to your browser. In some cases, a string literal has been output. In others, strings have first been concatenated or variables have been evaluated. I’ve also shown output spread over multiple lines. But there is also an alternative to echo that you can use: print. The two commands are quite similar, but the print is a function-like construct that takes a single parameter and has a return value (which is always 1), whereas echo is purely a PHP language construct.
Because both commands are constructs, neither requires the use of parentheses.By and large, the echo command will be a tad faster than print in general text output, because it doesn’t set a return value. On the other hand, because it isn’t implemented as a function, echo cannot be used as part of a more complex expression, whereas
print can. Here’s an example of output whether the value of a variable is TRUE or FALSE
using print, something you could not perform in the same manner with echo because it would display a Parse error message:

$ b? print "TRUE" : print "FALSE";

The question mark is simply a way of interrogating whether variable $b is TRUE or FALSE. Whichever command is on the left of the following colon is executed if $b is TRUE, whereas the command to the right is executed if $b is FALSE.
Generally, though, the examples in this book use echo, and I recommend that you do so as well until you reach such a point in your PHP development that you discover the need for using print.

<< Prev Next >>

Loading