PHP Basics for Tags, Statements, and Comments
Start a PHP block with <?php and close it with ?> when returning to HTML in the same file. Statements such as an assignment or echo usually end in ;. The examples below let you run both a mixed page in a browser and a PHP-only script in a terminal.
Mark where PHP begins and ends
Put whitespace or a newline after <?php. HTML outside the tags is passed through. <?= value ?> is the short form of echo and works independently of short_open_tag. See the PHP manual on tags.
The bare <? tag depends on configuration. Use the full opening tag in new code. PHP 7 removed <% … %> and <script language="php">. Update those tags and check the resulting output when porting old code. See the PHP 7 migration notes.
Run PHP inside an HTML page
With PHP installed, make an empty folder. Save the complete example in it as index.php, in UTF-8 without a byte order mark (BOM).
<?php
$message = 'PHP & HTML practice';
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>PHP basic syntax example</title>
</head>
<body>
<main>
<h1>PHP basic syntax example</h1>
<p><?= htmlspecialchars($message, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') ?></p>
</main>
</body>
</html>
Open a terminal in that folder. The first command checks the syntax without running the file. The second prints the generated HTML source to the terminal.
php -l index.php
php index.php
To view the rendered page, start the built-in server in the same folder. Use this server for local development.
php -S 127.0.0.1:8000
Open http://127.0.0.1:8000/. The page shows the heading and PHP & HTML practice. Press Ctrl+C in the terminal to stop the server. If port 8000 is busy, change both the command and the address to 8001. See the PHP manual on the built-in server.
The <?= tag prints the value. In this example, htmlspecialchars() turns & into & in the response so the browser displays a literal ampersand. Use this form to print variables as text in an HTML body.
Terminate statements with semicolons
A new line normally does not terminate a statement. Put ; after an assignment or an echo statement. The closing ?> can terminate the statement directly before it. If you omit the closing tag in a PHP-only file, retain the last semicolon. See the PHP manual on statement separation.
Save this example as syntax.php. Omitting the closing tag in a PHP-only file prevents stray whitespace after it from becoming output.
<?php
// Print the first string.
echo 'Lorem', PHP_EOL;
# This line is a comment.
/*
* A block comment can span multiple lines.
* The next statement is outside the comment.
*/
echo 'Ipsum', PHP_EOL;
Lint and run the file. The two strings appear on separate lines because PHP_EOL inserts the platform’s line ending.
php -l syntax.php
php syntax.php
Lorem
Ipsum
Write single-line and block comments
// and # start single-line comments. Use /* … */ for a block comment. The contents are not printed. A block comment ends at the first */, so keep block comments from nesting. See the PHP manual on comments.
In a mixed file, ?> ends PHP mode even after // or # on the same line. The HTML that follows is still sent in the response. Remove that HTML or handle it separately if it should be hidden.
Diagnose source output and syntax errors
- If the terminal cannot find
php, check the installation and command path, then runphp -v. Check that the example was saved with a.phpextension and without.txtat the end. - If the browser shows the source, check whether the address starts with
file://. Open the example through the PHP server address. - For syntax errors, inspect the reported line and the line above it for a quote, bracket, or semicolon. Run
php -l index.phpagain. Linting catches syntax errors before execution. It cannot catch failures that occur only at runtime.
