echo("test") won't do any harm either). They output the string representation of a variable, constant, or expression. They can't be used to print arrays or objects.
Assign the string
Joel to the variable
$name = "Joel";
Output the value of $name using
echo $name; #> Joel print $name; #> Joel
Parentheses are not required, but can be used
echo($name); #> Joel print($name); #> Joel
Using multiple parameters (only
echo $name, "Smith"; #> JoelSmith echo($name, " ", "Smith"); #> Joel Smith
echo, is an expression (it returns
1), and thus can be used in more places:
print("hey") && print(" ") && print("you"); #> you11
The above is equivalent to:
print ("hey" && (print (" " && print "you"))); #> you11
When outside of PHP tags, a shorthand notation for
echo is available by default, using
<?= to begin output and
?> to end it. For example:
<p><?=$variable?></p> <p><?= "This is also PHP" ?></p>
Note that there is no terminating
;. This works because the closing PHP tag acts as the terminator for the single statement. So, it is conventional to omit the semicolon in this shorthand notation.
and operators and has left association.
echo '1' . print '2' + 3; //output 511
Same example with brackets:
echo '1' . print ('2' + 3); //output 511
In short, there are two main differences:
echocan have multiple parameters.