Difference between Echo and Print in PHP
Key difference: An ‘echo’ and ‘print’ function is a language construct in PHP language. An ‘echo’ gives an output in one or more strings, while a ‘print’ gives the output only in one string.
include("ad4th.php"); ?>Both, ‘echo’ and ‘print’ are not real functions in PHP, rather they are language constructs.
The ‘echo’ function is basically used to represent or command for an output string in PHP. Unlike the other output functions in various languages, this function does not actually behave like a function, instead it is a real construct, which behaves like a function. The ‘echo’ does not require any enclosing parentheses; hence the parameters to the command are passed without the parentheses. It also has a shortcut syntax, which immediately follows the opening tag with an equal sign.
The ‘echo’ syntax is as follows:
void echo ( string $arg1 [, string $... ] )
include("ad3rd.php"); ?>For example:
<?php
$str = "Hello world!";
echo $str;
?>
The ‘print’ function can be executed with or without the parentheses. Unlike ‘echo’, even this is not a pure function and is a construct in the PHP language. A ‘print’ returns the value along with the executed output. While executing the ‘print’ function, the programmer is limited to pass only one argument.
The ‘print’ syntax is followed as:
int print ( string $arg )
For example:
<?php
print "<h2>PHP is fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn my first print function !";
?>
Comparison between Echo and Print in PHP:
|
‘echo’ in PHP |
‘print’ in PHP |
Types |
This is a type of output string. |
This is also a type of output string. |
Written as |
It is not written with parentheses. |
It can or cannot be written with parenthesis. |
Number of strings |
Echo can output one or more strings. |
Print can only output one string, and returns always 1. |
Functionality |
‘echo’ functions faster as compared to ‘print’. |
‘print’ compared to ‘echo’ is slower in function. |
In argument wise |
echo ($arg1 [, $arg2 ... ] ) |
print ($arg) |
Image Courtesy: stackoverflow.com, itexplore.net
Add new comment