What is the difference between PHP echo and print ?
In PHP, the two basic constructs are used to get outputs which are echo and print. If firs of all we talk about echo it is the most basic way to display the result of program in php.
echo:
echo is not a kind of function,Because it is a language construct, hence, you can use it with or without parentheses such as echo or echo(). It do not return any value.
With the help of echo, we can display the value of a variable, string with single quotes or with double quotes and multiple strings separated by commas.
For Example:
<?php
$myString1 = "Welcome to CODEHUNGER!";
$myString2 = 'I am echo statement';
echo $myString1.'<br>';
echo $myString2;
echo "<h5>I love using PHP!</h5>";
?>
Output:
Print:
print is not a function. It is also a language construct like echo but it is always return a value either 0 or 1 so, we can use it to print the result of any regular expression.As compared to echo, the execution of print is slow. It can display only one string at a time.
For example:
<?php
$myString1 = "Welcome to CODEHUNGER!";
$myString2 = 'I am developer';
print($myString1);
print '<br>'.$myString2;
print "<h5>I love using PHP! print by print statements</h5>";
?>
output:
Difference on key points in table format
echo | |
---|---|
echo is display strings and variable ,echo is not a really function but a language construct | print is display strings and variable ,print is not a really function but a language construct. |
It Support with or without parentheses | it also like as echo Support with or without parentheses |
echo can pass multiple string separated as ( $var1,$var2 ). for example: echo $var1,$var2; | using print can doesn’t pass multiple argument i.e we can only pass one variable For example: print($var); |
echo is faster then print | it is slower than echo |
echo doesn’t return any value | print always return 1 |