PHP code lives inside tags. Anything outside the tags is sent to the browser unchanged. Anything inside is run as PHP.
The opening and closing tags
<p>This is plain HTML.</p>
<?php
echo "<p>This line was written by PHP.</p>\n";
?>
<p>Back to plain HTML.</p>
<p>This is plain HTML.</p>
<p>This line was written by PHP.</p>
<p>Back to plain HTML.</p>
<?php starts PHP mode. ?> ends it. You can switch between HTML and PHP as many times as you like in one file.
The short echo tag
When you only want to print one value, <?= is a short way to write <?php echo.
<?php $city = "Mumbai"; ?>
<p>Weather in <?= $city ?></p>
<p>Weather in Mumbai</p>
The short echo tag is always available. It is common in templates.
Files that contain only PHP
If a file contains only PHP, leave out the closing ?> tag. The standard coding style does the same, and it avoids a classic bug.
<?php
// config.php: nothing but PHP, so no closing tag
$siteName = "My Blog";
Why? Any space or blank line after ?> is sent to the browser as output. That stray output can break features that must happen before any output, such as redirects and cookies.
Tags you should not use
<?(the short open tag) depends on a setting that is often off. Do not use it.<script language="php">and<% %>were removed long ago.
A space after the closing tag
?> at the end of an included file can cause the error “headers already sent”. Leaving out the final ?> prevents it.