PHP can be used to add content to HTML files. While HTML is processed directly by a web browser, PHP scripts are executed by a web server and the resulting HTML is sent to the browser.
The following HTML markup contains a PHP statement that will add Hello World!
to the output:
<!DOCTYPE html>
<html>
<head>
<title>PHP!</title>
</head>
<body>
<p><?php echo "Hello world!"; ?></p>
</body>
</html>
When this is saved as a PHP script and executed by a web server, the following HTML will be sent to the user's browser:
<!DOCTYPE html>
<html>
<head>
<title>PHP!</title>
</head>
<body>
<p>Hello world!</p>
</body>
</html>
echo
also has a shortcut syntax, which lets you immediately print a value. Prior to PHP 5.4.0, this short syntax only works with the short_open_tag configuration setting enabled.
For example, consider the following code:
<p><?= "Hello world!" ?></p>
Its output is identical to the output of the following:
<p><?php echo "Hello world!"; ?></p>
In real-world applications, all data output by PHP to an HTML page should be properly escaped to prevent XSS (Cross-site scripting) attacks or text corruption.
See also: Strings and PSR-1, which describes best practices, including the proper use of short tags (<?= ... ?>
).