Difference between revisions of "PHP text generation"

From Wiki @ Karl Jones dot com
Jump to: navigation, search
(print)
Line 15: Line 15:
 
== echo() ==
 
== echo() ==
  
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 60: Line 60:
  
 
* [[PHP]]
 
* [[PHP]]
 +
* [[PHP variable]]
  
 
== External links ==
 
== External links ==

Revision as of 09:01, 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.

echo

A simple echo:

<?php
echo "Hello World";
?>

echo()

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.

print

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