Difference between revisions of "PHP text generation"
Karl Jones (Talk | contribs) |
Karl Jones (Talk | contribs) (→echo()) |
||
| (2 intermediate revisions by the same user not shown) | |||
| Line 13: | Line 13: | ||
</pre> | </pre> | ||
| − | == echo | + | == echo multiple parameters == |
| − | You can use <code>echo</code> with multiple parameters, to combine text and variables. | + | You can use <code>echo</code> with multiple parameters, to combine text and [[PHP variable|PHP variables]]. |
The example below uses two variables named <code>$word1</code> and <code>$word2</code>. | The example below uses two variables named <code>$word1</code> and <code>$word2</code>. | ||
| 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 == | ||
* [[PHP]] | * [[PHP]] | ||
| + | * [[PHP variable]] | ||
== External links == | == External links == | ||
Latest revision as of 16:39, 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 multiple parameters
You can use echo with multiple parameters, to combine text and PHP 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