echo and print

suggest change

echo and print are language constructs, not functions. This means that they don’t require parentheses around the argument like a function does (although one can always add parentheses around almost any PHP expression and thus 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.

$name = "Joel";
echo $name;   #> Joel
print $name;  #> Joel
echo($name);  #> Joel
print($name); #> Joel
echo $name, "Smith";       #> JoelSmith
echo($name, " ", "Smith"); #> Joel Smith
print("hey") && print(" ") && print("you"); #> you11
print ("hey" && (print (" " && print "you"))); #> you11

Shorthand notation for echo

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.

Priority of print

Although the print is language construction it has priority like operator. It places between = += -= *= **= /= .= %= &= and and operators and has left association. Example:

echo '1' . print '2' + 3; //output 511

Same example with brackets:

echo '1' . print ('2' + 3); //output 511

Differences between echo and print

In short, there are two main differences:

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Outputting the value of a variable:
* echo and print

Table Of Contents
2 Arrays
4 Types
10 Cookies
14 JSON
15 SOAP
17 cURL
19 XML
21 Traits
35 UTF-8
36 URLs
38 PHPDoc
41 Loops
44 Closur
72 YAML
77 Cache
78 Streams
81 PDO
82 SQLite3
83 Sockets
84 Outputting the value of a variable
87 MongoDB
93 IMAP
94 Redis
95 Imagick
102 APCu
108 PSR