Difference between revisions of "PHP text generation"
Karl Jones (Talk | contribs) |
Karl Jones (Talk | contribs) (→print) |
||
Line 54: | Line 54: | ||
PHP has has a <code>print</code> statement, which behaves like <code>echo</code>. | PHP has has a <code>print</code> statement, which behaves like <code>echo</code>. | ||
+ | |||
+ | It offers no advantages over <code>echo</code>, and is not commonly used. | ||
== See also == | == See also == |
Revision as of 08:59, 29 September 2016
The PHP programming language provides a keyword named echo
which generates text.
In a PHP-based web application, text generated by echo
becomes part of the response that the web server return to the client.
Contents
echo
A simple echo
:
<?php echo "Hello World"; ?>
echo()
You can use echo
with multiple parameters, to combine text and variables.
The example below uses two variables named $word1
and $word2
.
<?php $word1 = "Hello"; $word2 = "World"; echo $word1, " ", $word2; ?>
Shortcut syntax
PHP also provides a shortcut for echo
:
$msg = "Hello World"; This is HTML, lorem ipsum, <?= $msg ?>, more HTML etc.
Shortcuts may be unavailable (switch off) on some servers. Using shortcuts may not be best practice.
Using parentheses with echo ()
echo
is sometimes used with parentheses:
<?php echo ( "Hello World" ); ?>
This does not work when using multiple parameters.
There is no reason to prefer parentheses; they add nothing, so far as I have discovered.
PHP has has a print
statement, which behaves like echo
.
It offers no advantages over echo
, and is not commonly used.
See also
External links
- echo @ php.net