PHP Closing Tags: Leave Them Out Or Out#
The Short Answer#
👉 If your file contains only PHP code, it’s best practice to omit the closing ?> tag.
Why Leave It Out?#
The reason is surprisingly simple:
Any whitespace, newlines, or hidden characters (like a BOM) after a closing
?>tag are sent directly to the browser as output.
[!NOTE]
BOM stands for Byte Order Mark - It’s a special, invisible sequence of bytes (
EF BB BF) that can appear at the very beginning of a text file when it’s saved with UTF-8 encoding with BOM.Its original purpose was to tell programs: “Hey, this file is encoded in UTF-8.”
This can trigger the infamous “headers already sent” error, break JSON APIs, or cause strange blank lines in your HTML.
Frameworks like Laravel, WordPress, and even the official PHP manual recommend leaving it out for clean, bug-free code.
When to Keep It#
There are cases where you still need the closing tag:
If your file mixes PHP and HTML (e.g., a template file), the interpreter needs to know when to leave PHP mode and return to raw HTML.
Example:
<?php echo "Hello, world!"; ?>
<p>This is plain HTML again.</p>
But in files that contain only PHP logic—like database connection scripts, configuration files, or class libraries—closing tags are unnecessary and risky.
Wisdom Bite: The Door Analogy#
Think of ?> as leaving a door open at the end of your script.
With the door open, stray characters, invisible spaces, or editor quirks can slip in.
Leaving the tag out keeps the door shut and your script safe from accidental leaks.
The Role of ?> in PHP#
The closing tag is optional in PHP-only files.
The interpreter automatically stops parsing at the end of the file.
The official recommendation: leave the closing tag off in pure PHP files.
Why It Matters with require and include#
When you include a file, whatever is in that file is dropped straight into your running script.
With
?>at the end: If there’s an extra space, newline, or BOM after it, PHP treats it as literal output.Headers may fail (“headers already sent”).
JSON or XML output can become invalid.
HTML may display stray whitespace.
Without
?>: The file ends cleanly, nothing slips through, and you avoid hidden output entirely.
👉 That’s why best practice is to omit ?> in PHP-only files, especially those that are required or included.
Before & After Example (Great for Class)#
Problematic file (dbconnect.php):
<?php
$pdo = new PDO("mysql:host=localhost;dbname=test", "user", "pw");
?>
(But the editor sneaks in a newline here ⬇)
Main script:
<?php
require 'dbconnect.php';
header("Content-Type: application/json");
echo json_encode(["status" => "ok"]);
➡️ Result: ❌ “Headers already sent” error.
Fixed file (dbconnect.php):
<?php
$pdo = new PDO("mysql:host=localhost;dbname=test", "user", "pw");
No closing tag → no trailing whitespace → clean output. ✅
Best Practice (Takeaway)#
Always omit
?>in PHP-only files.Use it only when you must return to HTML mode.
This avoids accidental output and saves you from hard-to-track bugs.