Skip to content

PHP Tutorial Getting Started with PHP

PHP Tags

Lesson 10 of 369 2 min read By Deepak Paswan Updated Oct 5, 2026

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

index.php
PHP
<p>This is plain HTML.</p>
<?php
echo "<p>This line was written by PHP.</p>\n";
?>
<p>Back to plain HTML.</p>
Output
OUTPUT
<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.

index.php
PHP
<?php $city = "Mumbai"; ?>
<p>Weather in <?= $city ?></p>
Output
OUTPUT
<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.

index.php
PHP
<?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.
warning
A space after the closing tag
A single space after ?> at the end of an included file can cause the error “headers already sent”. Leaving out the final ?> prevents it.