PHP text generation

From Wiki @ Karl Jones dot com
(Redirected from Generating text (PHP))
Jump to: navigation, search

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 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.

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