PHP echo, PHP print

echo and print are used to get output in PHP.

(1) PHP echo

PHP echo is used to display statements in PHP in the output.

More than one parameter in echo statements is seperate and pass by ” , ” (comma) and ” . ” (dot).

Echo is used with parentheses ( ) or is used without it.

Example

<?php
$a = "Hello";
echo $a;
echo "Welcome to Coding Bot";
?>

Output

Hello
Welcome to Coding Bot

You can not define any HTML tags inside PHP’s opening and closing tags. Doing this results in error generation. But via PHP echo, you can also render HTML by displaying it. For this, you define HTML tags as strings in quotes inside echo.

echo "<tagName>Your text here</tagName>";

Example

<?php echo "<h3>Welcome to Tutorials Track</h3>"; ?> 

Output

Welcome to Tutorials Track

(2) PHP Print

Print like php echo is also a language construct. This is not a function.

PHP Print is a traditional way of displaying output. If you are familiar with c language then print will look more suitable to you.

PHP prints behaves like a function. If you want the output to be used by the function or reference of the function, in such a situation, you should use the PHP prints only.

The general syntax of PHP prints is being given below.

print "your text here";

PHP prints can also be used with parenthesis.

print ("your text here");

HTML can also be define in print as echo.

print "<tagName>your text here</tagName>"

How is echo different from print

In PHP, echo and print are similar in Uses but some important differences are found in this. These are being described below.

PHP echo does not return any value. You can not use it in the context of the function. While PHP print returns 1. You can use it in expressions.

Return Value

Example

<?php
$name = "Rohan";
if ($name == 1)
   {
    print "Hello";
   }
?>

No Multiple Arguments

echo can process multiple arguments. For this, you define all arguments in commas by quoting ( , ).

echo “Hii”,”I am Jon”,$name;

But PHP print only processes one argument at a time. In PHP prints you can not print 2 strings at a time by separating comma ( , ).

print "Hii","I am Jon";     // Invalid
print "Hii I am Jon";       //Valid