PHP Reference ยท Concepts and Reference
PHP tags and embedding PHP in HTML
Reference for PHP opening and closing tags: the standard tag, the short echo tag, mixing PHP with HTML, the newline after a closing tag and why to omit it.
PHP only runs the code inside PHP tags. Everything outside the tags is passed through to the output unchanged. That is what lets one file mix HTML and PHP: the server sends the HTML as written and replaces each PHP block with whatever it prints.
There are two tag forms you should use: the standard tag <?php ... ?> and the short echo tag <?= ... ?>, which is a shortcut for <?php echo ...; ?>. Both are always available. The old short tag <? depends on the short_open_tag setting, and <script language="php"> and <% %> were removed in PHP 7.
<h1><?= htmlspecialchars($title) ?></h1> <?php if ($loggedIn): ?> <p>Welcome back.</p> <?php endif; ?>
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
The tag forms
| Tag | Meaning |
|---|---|
<?php ... ?> |
Standard tag, always works. |
<?= expression ?> |
Short echo tag: prints one or more comma-separated expressions. Always available since PHP 5.4. |
<?php at the end of a file |
A pure-PHP file needs no closing tag. |
<? ... ?> |
Short open tag. Only when short_open_tag=On. Avoid it, it clashes with XML declarations. |
Rules for closing tags
?>ends PHP mode and also acts as a statement terminator, so the last statement before it may omit its semicolon.- A single newline directly after
?>is removed. Add a blank line if you want a line break in the output. - A
?>inside a//or#comment ends the PHP block, because the closing tag wins over the comment. - Omit the closing tag in files that contain only PHP (classes, config, helpers). Stray whitespace after
?>would be sent as output and can breakheader()calls with "headers already sent" errors. PSR-12 requires this.
Usage notes
- The colon syntax (
if (...):,foreach (...):,endif;,endforeach;) reads better than braces in templates that mix HTML. <?=takes expressions, not statements, and does not escape anything. Escape output withhtmlspecialchars()for text that comes from users.- A file that starts with HTML before
<?phpis fine in templates, but not in a file that must callheader()orsession_start()first. declare(strict_types=1);must be the first statement in the file after the opening tag.- Template engines such as Blade and Twig compile to ordinary PHP files that use these same tags.
- In CLI scripts, text before the first
<?phpis printed too; a shebang line (#!/usr/bin/env php) is the one special case and is skipped.
Related
- PHP comments
- PHP command line
- Tutorial: PHP and HTML, PHP syntax, How PHP works