There are some differences between echo and print:
Tip: echo is marginally faster compared to print as echo does not return any value.
echo is a language construct and can be used with or without parentheses: echo or echo().
Display Strings
The following example shows how to display different strings with the echo command (also notice that the strings can contain HTML markup):
<?php
echo "<h2>Welcome To PHP ! </h2>";
echo "Hello Php ! <br>";
echo "I am about to learn web development!<br>";
echo "This","string","was","made","with multiple parameters.";
?>
Display Variables
The following example shows how to display strings and variables with the echo command:
<?php
$variable="PHP";
$variable2="easytolearning.com";
$food=array("Pizza","Cake","Pasty");
echo $variable;
echo "<br>";
echo "Study PHP at $variable2";
echo "My Food is a {$food[0]}";
?>
print is also a language construct and can be used with or without parentheses: print or print().
Display Strings
The following example shows how to display different strings with the print command (also notice that the strings can contain HTML markup):
<?php
print "<h2>PHP !</h2>";
print "Hello world!<br>";
print "I am about to learn PHP!";
?>
Display Variables
The following example shows how to display strings and variables with the print command:
<?php
$variable="PHP";
$variable2="easytolearning.com";
$food=array("Pizza","Cake","Pasty");
print $variable;
print "<br>";
print "Study PHP at $variable2";
print "My Food is a {$food[0]}";
?>
© 2024 Easy To Learning. All Rights Reserved | Design by Easy To Learning