PHP Delimiters Explained: PHP Opening and Closing Tags

PHP delimiters are the markers that tell the PHP interpreter where PHP code starts and ends. If you have worked with PHP templates, you have probably typed <?php hundreds of times without giving much thought to it. That small tag is doing an important job: separating PHP instructions from regular HTML.

A missing delimiter is one of those tiny mistakes that can waste a surprising amount of debugging time. The browser may simply show your PHP code as plain text, or the page may fail in a way that makes you wonder if PHP itself has stopped working.

In this article, we will look at the different PHP delimiters, when to use them, and the common mistakes to avoid.

Quick Answer: What are PHP delimiters?

PHP delimiters are special tags used to mark PHP code inside a file. The standard PHP opening and closing delimiters are:

<?php

// PHP code goes here

?>

The opening delimiter <?php tells PHP to start interpreting the following content as PHP code. The closing delimiter ?> tells PHP where the PHP block ends.

PHP files can contain multiple PHP blocks mixed with HTML:

<h1>User Profile</h1>

<?php
$name = "John";
echo $name;
?>

<p>This is normal HTML content.</p>

Standard PHP opening and closing tags

The recommended PHP delimiter syntax uses the full opening tag:

<?php

echo "Hello PHP";

?>

The <?php opening tag is always available and works regardless of server configuration. It is the only opening tag you should use for production applications.

The closing tag ?> is optional when a PHP file contains only PHP code. In fact, many modern PHP projects intentionally omit it.

For example, this is perfectly valid:

<?php

$message = "Hello PHP";

echo $message;

Leaving out the closing tag avoids accidental whitespace or invisible characters being sent to the browser after the PHP code ends. This can prevent common issues such as the headers already sent error when using functions like header().

For more details about PHP language syntax, the official PHP documentation provides the complete reference: PHP Tags in the PHP Manual.

Short echo tag in PHP

PHP provides a shorter delimiter for directly printing values. This syntax is commonly used with the PHP echo statement when generating output inside HTML templates.

<?= $variable ?>

This is called the short echo tag. It is a shorter version of:

<?php echo $variable; ?>

For example:

<?php
$title = "PHP Delimiters";
?>

<h1><?= $title ?></h1>

The short echo tag is commonly used in PHP templates because it keeps HTML cleaner. Since PHP 5.4, it is always available and does not depend on the short_open_tag configuration setting.

It is a good choice when you only need to output a value. However, for larger blocks containing logic, use the normal <?php opening tag.

Short PHP opening tags

Older PHP versions supported another opening tag:

<?

echo "Hello PHP";

?>

This syntax is called the short open tag. It is not recommended because its availability depends on the short_open_tag PHP configuration setting.

If short tags are disabled on a server, PHP will treat the code as normal text instead of executing it.

For example, this may display directly in the browser:

<?

echo "Hello PHP";

?>

The official recommendation is to avoid short open tags and always use:

<?php

echo "Hello PHP";

?>

This makes your code more portable when moving between local development environments, shared hosting, and production servers.

ASP-style PHP tags (removed syntax)

Older PHP versions also supported ASP-style delimiters:

<%
echo "Hello PHP";
%>

These tags were removed from PHP 7.0 and should not be used in modern applications.

If you maintain an old PHP project containing these delimiters, replace them with the standard PHP tags:

<?php

echo "Hello PHP";

?>

PHP delimiter best practices

Choosing the correct PHP delimiter may look like a small decision, but consistent usage prevents portability problems and makes your code easier to maintain.

1. Always use the full PHP opening tag

Use:

<?php

Avoid:

<?

The full opening tag works on every PHP installation and avoids unexpected behavior caused by server configuration differences.

2. Omit the closing tag in PHP-only files

For files that contain only PHP code, leaving out the closing delimiter is considered a good practice.

Example:

<?php

function calculateTotal($price, $tax)
{
    return $price + ($price * $tax);
}

This avoids accidental spaces, blank lines, or hidden characters after the closing tag. These extra characters can be sent as output and may cause problems when PHP tries to send HTTP headers later. You can read more about this behavior in the guide on PHP header handling.

For example, this type of error can happen when whitespace exists before a redirect:

<?php

echo " ";

header("Location: dashboard.php");

The browser may receive output before the header() call, causing PHP to report a headers already sent warning.

3. Use short echo tags only for output

Short echo tags make templates easier to read:

<h2><?= $userName ?></h2>

However, avoid putting complex logic inside them. Keep calculations, database operations, and business rules inside regular PHP blocks.

A cleaner approach is:

<?php

$displayName = getUserName();

?>

<h2><?= $displayName ?></h2>

Common PHP delimiter errors and fixes

PHP code is displayed in the browser

Problem: You see PHP code instead of the expected output.

Possible causes:

  • The file does not have the .php extension.
  • The PHP server is not configured correctly.
  • You used a short opening tag (<?) that is disabled.

Fix: Use the standard opening tag:

<?php

echo "PHP is running";

Unexpected output before HTML headers

Problem: PHP shows an error such as:

Cannot modify header information - headers already sent

Possible cause: Extra whitespace or text was sent before a function like header(), setcookie(), or session_start().

Fix:

  • Remove unnecessary closing PHP tags from PHP-only files.
  • Check for blank lines before the opening tag.
  • Save files without unwanted BOM characters.

PHP delimiters inside HTML templates

One of the reasons PHP became popular is its ability to mix server-side code with HTML. PHP delimiters make this possible by allowing you to switch between PHP and HTML whenever required.

For example, a typical template may look like this:

<!DOCTYPE html>
<html>
<head>
    <title>User Profile</title>
</head>
<body>

<h1>Profile</h1>

<?php
$user = "David";
?>

<p>Welcome, <?= $user ?></p>

</body>
</html>

Notice that PHP is opened only where it is needed. The rest of the file remains normal HTML.

This style is easier to maintain than generating the entire page using PHP echo statements:

<?php

echo "<h1>Profile</h1>";
echo "<p>Welcome, David</p>";

For small templates, mixing PHP and HTML is usually the simpler approach. For larger applications, developers often use template engines or frameworks, but the underlying concept remains the same: delimiters separate executable PHP code from output content.

PHP delimiter FAQ

Can I use PHP without a closing delimiter?

Yes. A PHP file does not require the closing ?> tag. Omitting it is recommended for files that contain only PHP code.

Are PHP short tags still supported?

The short opening tag <? is still supported in some PHP installations, but it depends on the short_open_tag configuration. Avoid using it in new projects because your code may fail on another server.

Is the PHP echo tag safe to use?

Yes. The short echo tag <?= is safe to use in modern PHP versions and has been always enabled since PHP 5.4.

What is the recommended PHP delimiter?

Use <?php for PHP code blocks and <?= when directly displaying a value. Avoid short open tags and removed legacy tags.

Conclusion

PHP delimiters are simple, but using the right ones avoids many unnecessary problems. The standard <?php tag should be your default choice for PHP code, while <?= is useful for clean output inside templates.

A small habit like avoiding unreliable short tags and unnecessary closing tags can make your PHP code easier to move, maintain, and debug across different environments.

Photo of Vincy, PHP developer
Written by Vincy Last updated: July 22, 2026
I'm a PHP developer with 20+ years of experience and a Master's degree in Computer Science. I build and improve production PHP systems for eCommerce, payments, webhooks, and integrations, including legacy upgrades (PHP 5/7 to PHP 8.x).

Continue Learning

These related tutorials may help you continue learning.

2 Comments on "PHP Delimiters Explained: PHP Opening and Closing Tags"

Leave a Reply

Your email address will not be published. Required fields are marked *

Explore topics
Need PHP help?