Question

PHP Short Tags Explained: Should You Use `<?` and `<?=` in PHP?

phpphp-shorttags

Question

I read in the PHP documentation that PHP supports multiple opening and closing tag styles:

<?php ?>
<script language="php"></script>
<? ?>
<% %>

According to the documentation, <?php ?> and <script language="php"></script> are always available, while short tags and ASP-style tags can be enabled or disabled through php.ini. Because of that, the documentation says they are less portable and generally not recommended.

In practice, I often see servers with short tags enabled. Writing:

<?= $value ?>

or even:

<? echo $value; ?>

feels more convenient than writing:

<?php echo $value; ?>

Since developer convenience matters, why are PHP short tags generally not recommended, and when is it acceptable to use them?

Short Answer

By the end of this page, you will understand what PHP short tags are, why portability matters more than a few saved keystrokes, and why <?php ... ?> is usually the safest choice. You will also learn the special case of <?= ... ?>, how teams handle PHP tags in real codebases, and what to avoid in production code.

Concept

PHP code is embedded inside files using opening and closing tags. The most common and safest form is:

<?php
// PHP code here
?>

Historically, PHP also supported shorter alternatives such as:

<?
// PHP code
?>

and shorthand output tags such as:

<?= $name ?>

The core issue is portability. A portable PHP file works the same way across different servers and configurations. Short open tags like <? depend on server configuration in older PHP environments, so code that works on one machine may fail on another.

That is why <?php is recommended:

  • It is explicit.
  • It is always recognized.
  • It avoids configuration surprises.
  • It makes the file clearly identifiable as PHP.

This matters in real programming because code is rarely written for just one machine. It may run on:

  • a local development environment
  • a teammate's laptop
  • a staging server
  • a production server
  • a CI pipeline
  • a shared hosting environment

If one of those environments disables short tags, code using <? may break or output raw PHP source as plain text.

There is one important distinction:

  • <?php is the standard full opening tag.
  • <? is the classic short open tag and is the problematic one.
  • <?= is the short echo tag. In modern PHP, this form is widely supported and commonly accepted in templates.

So the real recommendation is usually avoid <?, and prefer <?php, while <?= may be acceptable depending on project style and PHP version requirements.

Mental Model

Think of PHP tags like power plug shapes.

  • <?php is the standard plug that works everywhere.
  • <? is a special plug adapter that only works in places where the outlet is configured for it.
  • <?= is a compact switch used for one very specific job: outputting a value.

If you are building something only for your own desk, you might accept a special adapter. But if you want your code to travel safely between machines and teams, you use the standard plug.

Saving a few keystrokes is helpful, but reliability usually matters more than typing speed in shared or long-lived code.

Take Quiz

Syntax and Examples

The main PHP tag styles you may encounter are:

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

Short open tag:

<?
$name = "Ava";
echo $name;
?>

Short echo tag:

<?= $name ?>

Recommended syntax

Use <?php for normal PHP blocks:

<?php
$total = 10;
$tax = 2;
echo $total + $tax;

Use <?= only when you want to print a value directly, especially in view templates:

<p>Hello, <?= htmlspecialchars($name) ?></p>

This is concise and readable in HTML templates.

Beginner-friendly example

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

<h1>Welcome, <?= htmlspecialchars($user) ?></h1>

What this does

  • <?php ... ?> runs regular PHP code.
  • $user = "Sam"; stores a value.
  • <?= ... ?> outputs the value into the HTML.
  • htmlspecialchars() protects output when rendering HTML.

This pattern is common in PHP templates because it keeps logic and output clear.

Step by Step Execution

Consider this example:

<?php
$product = "Notebook";
$price = 12;
?>

<p>Product: <?= htmlspecialchars($product) ?></p>
<p>Price: $<?= $price ?></p>

Step by step:

  1. PHP enters the <?php block.
  2. $product is assigned the string "Notebook".
  3. $price is assigned the number 12.
  4. The ?> closes the PHP block.
  5. The HTML <p>Product: is sent to the browser.
  6. <?= htmlspecialchars($product) ?> prints Notebook.
  7. The closing </p> is sent.
  8. The second paragraph starts.
  9. $ is plain text in the HTML output.
  10. <?= $price ?> prints 12.
  11. The browser receives:
<p>Product: Notebook</p>
<p>Price: $12</p>

If you replaced the first <?php with <? on a server that does not support short open tags, the PHP code might not run correctly. That is exactly the portability problem.

Real World Use Cases

PHP tags appear most often in these situations:

1. Rendering HTML templates

<h2><?= htmlspecialchars($article['title']) ?></h2>

Useful for outputting small values inside markup.

2. Building server-rendered pages

<?php if ($isLoggedIn): ?>
    <a href="/logout">Logout</a>
<?php else: ?>
    <a href="/login">Login</a>
<?php endif; ?>

This uses standard PHP tags for control flow in templates.

3. Legacy PHP applications

Older codebases may contain many <? tags because they were written when that style was common.

4. Shared hosting or mixed environments

Portability is critical here. A project may move between hosts with different PHP configurations, so <?php avoids breakage.

5. CMS themes and view files

In template-heavy code, <?= is often used because it is short and readable for output-only expressions.

Take Quiz

Real Codebase Usage

In real projects, teams usually follow simple rules:

Common team conventions

  • Use <?php for all PHP code blocks.
  • Allow <?= in templates if the project supports modern PHP.
  • Avoid <? completely.
  • Never rely on environment-specific parser settings unless the whole platform is controlled.

Why teams prefer explicit tags

Reliability

A repository should run the same way everywhere.

Readability

<?php is unmistakable. Someone scanning the file immediately knows a PHP block starts there.

Tooling support

Linters, formatters, code style tools, and IDEs work best with standard syntax.

Common patterns in codebases

View rendering

<li><?= htmlspecialchars($item['name']) ?></li>

Guarded output

<?php if (!isset($user)): ?>
    <p>Guest user</p>
<?php else: ?>
    <p><?= htmlspecialchars($user['name']) ?></p>
<?php endif; ?>

Validation before output

<?php
if (!isset($title)) {
    $title = 'Untitled';
}
?>

<h1><?= htmlspecialchars($title) ?></h1>

These examples show a practical compromise: standard tags for logic, short echo tags for output.

Common Mistakes

1. Using <? and assuming it works everywhere

Broken example:

<?
echo "Hello";
?>

Why it is a problem:

  • It may fail on environments where short open tags are disabled.
  • The code becomes less portable.

Safer version:

<?php
echo "Hello";
?>

2. Confusing <? with <?=

These are not the same.

  • <? starts a PHP block.
  • <?= outputs an expression.

Example:

<?= $name ?>

is equivalent to:

<?php echo $name; ?>

but this is not equivalent:

<? $name ?>

That code does not output anything useful.

3. Using short tags in long PHP-only files

If a file is mostly PHP logic, using short tags adds no benefit.

Less clear:

<?
function add($a, $b) {
    return $a + $b;
}

Better:

<?php
function add($a, $b) {
    return $a + $b;
}

4. Outputting raw data without escaping

Even if <?= is allowed, output should often be escaped.

Risky:

<p><?= $username ?></p>

Safer:

<p><?= htmlspecialchars($username) ?></p>

5. Optimizing keystrokes instead of maintainability

Saving a few characters is usually less important than:

  • portability
  • team consistency
  • easier debugging
  • fewer deployment surprises

Comparisons

SyntaxPurposePortableRecommendedNotes
<?php ... ?>Standard PHP blockYesYesBest default choice
<? ... ?>Short open tagNot alwaysNoCan depend on configuration
<?= ... ?>Short echo outputYes in modern PHPOften yesGood for templates
<script language="php"> ... </script>Alternative PHP blockRarely usedNoValid historically, uncommon in modern code
<% ... %>ASP-style tagsNoNoDeprecated/removed in modern PHP

<?php vs <?

  • <?php is explicit and reliable.
  • <? is shorter but may break across environments.

<?php echo vs <?=

  • <?php echo $name; ?> works everywhere standard PHP does.
  • <?= $name ?> is shorter and commonly used in templates.
  • For mixed HTML and small output expressions, <?= is often more readable.

Convenience vs portability

Concern<?<?php
Fewer keystrokesYesNo
Works across environmentsNot alwaysYes
Team-friendlyUsually noYes
Safe defaultNoYes
Take Quiz

Cheat Sheet

  • Use <?php for normal PHP blocks.
  • Avoid <? because it is less portable.
  • <?= $value ?> is shorthand for:
    <?php echo $value; ?>
    
  • In templates, <?= ... ?> is often acceptable.
  • In PHP logic files, prefer full <?php tags.
  • Escape HTML output when needed:
    <?= htmlspecialchars($value) ?>
    
  • Do not use ASP-style tags like <% %> in modern PHP.
  • Prefer team consistency over saving a few keystrokes.

Safe defaults

<?php
$value = "Hello";
?>

<p><?= htmlspecialchars($value) ?></p>

Rule of thumb

  • Logic: <?php
  • Output in templates: <?=
  • Never rely on <?

FAQ

Is <?= the same as <?php echo in PHP?

Yes. <?= $value ?> is shorthand for <?php echo $value; ?>.

Why is <? discouraged in PHP?

Because it may depend on server configuration, making code less portable across environments.

Is <?= safe to use in modern PHP?

Yes, in modern PHP it is widely supported and commonly used, especially in templates.

Should I use short tags in a team project?

Avoid <?. Use <?php for normal blocks. Use <?= only if your team's coding standards allow it.

Do short tags make PHP faster?

No meaningful performance benefit comes from using shorter tags. The main difference is readability and portability.

What is the best practice for PHP opening tags?

Use <?php as the default opening tag everywhere. Use <?= for concise output in HTML templates if appropriate.

Are short tags still found in legacy code?

Yes. Older PHP applications often used <?, but many teams replace them during maintenance for consistency and safety.

Take Quiz

Mini Project

Description

Create a small PHP profile card page that mixes PHP and HTML correctly. The project demonstrates when to use <?php for logic and <?= for output, while avoiding unsafe or non-portable <? tags.

Goal

Build a PHP page that displays a user's profile information using standard PHP tags and short echo tags safely.

Requirements

  1. Define user data in a <?php block.
  2. Render the user's name, role, and city inside HTML.
  3. Use <?= ... ?> for output inside the template.
  4. Escape all displayed text with htmlspecialchars().
  5. Do not use the <? short open tag.
Take Quiz

Keep learning

Related questions

View more