What does '<?=' mean in PHP?
Categories:
Understanding PHP's Short Echo Tag: <?=

Explore the <?= syntax in PHP, its functionality, benefits, and how it simplifies outputting variables and expressions.
In PHP, the <?= construct is known as the "short echo tag." It's a concise way to output the value of a variable or the result of an expression directly into your HTML. This tag is a shorthand for <?php echo, making your code cleaner and often more readable, especially within HTML templates. Understanding its usage is crucial for efficient PHP development.
What is the Short Echo Tag (<?=)?
The <?= tag was introduced in PHP 5.4 and is always enabled, regardless of the short_open_tag directive in php.ini. This means you can reliably use it in all modern PHP environments without configuration concerns. Its primary purpose is to quickly print the value of a variable, a function's return, or any valid PHP expression directly into the output stream.
<?php
$name = "Alice";
$age = 30;
// Traditional way
echo "Hello, " . $name . "! You are " . $age . " years old.<br>";
// Using the short echo tag
?>
Hello, <?= $name ?>! You are <?= $age ?> years old.<br>
<?=
// You can also use it for expressions
10 + 5
?>
<?=
// Or function calls
strtoupper("php")
?>
Comparing traditional echo with the short echo tag <?=
<?= is always enabled, the general <? (short open tag) is not. It's best practice to avoid <? for general PHP code to ensure maximum compatibility across different server configurations.Benefits and Use Cases
The main benefit of <?= is improved readability and conciseness, especially in template files where you're mixing HTML and PHP. It reduces the verbosity of <?php echo and makes the HTML structure clearer. It's particularly useful for displaying dynamic content like user names, dates, or calculated values directly within HTML elements.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><?= $pageTitle ?? 'Default Title' ?></title>
</head>
<body>
<h1>Welcome, <?= htmlspecialchars($userName) ?>!</h1>
<p>Today's date is: <?= date('Y-m-d') ?></p>
<p>Your cart total: $<?= number_format($cartTotal, 2) ?></p>
</body>
</html>
Practical use of <?= in an HTML template
flowchart TD
A[PHP Script Execution] --> B{Encounter `<?=`?}
B -- Yes --> C[Evaluate Expression/Variable]
C --> D[Output Result to HTML]
B -- No --> E[Continue Parsing HTML/PHP]
D --> EFlowchart illustrating how PHP processes the <?= tag
Important Considerations
While convenient, it's important to remember that <?= directly outputs its content. This means you should always sanitize or escape any user-generated content before outputting it to prevent Cross-Site Scripting (XSS) vulnerabilities. Functions like htmlspecialchars() are essential for this purpose.
<?php
$userInput = "<script>alert('XSS Attack!');</script>";
// DANGEROUS: Directly outputting user input
// echo "<p>Hello, " . $userInput . "</p>";
// ?>
// <p>Hello, <?= $userInput ?></p>
// SAFE: Using htmlspecialchars()
?>
<p>Hello, <?= htmlspecialchars($userInput) ?></p>
Demonstrating safe output with htmlspecialchars()
<?= or echo. Always use htmlspecialchars() or similar escaping functions to prevent security vulnerabilities like XSS.