PHP end tag "?>"

PHP end tag "?>"

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

PHP end tag "?>"

The behavior you are observing stems from how PHP handles file output by default and why removing the ?> delimiter changes that flow. When a PHP script executes, it treats everything between the opening <?php tag (or the start of the file) and the closing ?> tag as executable code. However, any text that appears after the ?> tag until the end of the file is treated strictly as plain text output.

In your specific scenario, the library file likely contains a closing ?> tag followed by some whitespace characters—newlines or spaces—before the end of the file. Because PHP outputs this trailing whitespace along with the rest of the response, these blank lines appear in your Ajax request result. This is not a bug in your application logic; it is a direct consequence of including content that falls outside the active PHP parsing block.

To understand why removing the tag fixes the issue, consider how the parser processes the file. Once the interpreter encounters ?>, it switches modes from executing code to emitting raw bytes. If there is even a single newline character after the tag, PHP sends that newline to the client. In many contexts, this is harmless because the browser or framework handles trailing whitespace gracefully. However, when sending JSON responses over Ajax, clients often parse the string as-is. Extra whitespace can cause validation errors in strict JSON parsers or inflate response sizes unnecessarily.

The industry standard practice for including libraries and utility files is to omit the ?> tag entirely if the file contains no closing HTML or echo statements. This convention eliminates the risk of accidental whitespace pollution. By removing the end tag, you ensure that every character in the file is processed as either code or intentional output, preventing silent issues like the blank lines you encountered.

This approach aligns with how many modern PHP frameworks structure their codebases. For instance, core files in projects like Laravel typically omit the closing delimiter from helper classes and configuration scripts unless they explicitly need to terminate output. This discipline avoids edge cases where whitespace after the tag might interfere with XML parsers, JSON decoders, or other sensitive downstream components.

If you are working on a legacy codebase where ?> tags are ubiquitous, the fix is straightforward: audit your included files for trailing whitespace and remove the closing tag unless it serves a specific purpose. You can verify the presence of invisible characters using a hex editor or by enabling error reporting to see if PHP warns about unexpected output.

While modern PHP versions (7.4 and later) include an output_buffering feature that helps mitigate some issues, relying on buffering as a fix for this specific problem is not recommended. Buffering adds overhead and complexity without solving the root cause of unintended whitespace emission. The most robust solution remains adhering to the rule: no closing tag in pure logic files.

If you are building new applications or refactoring existing ones, adopting this convention will save you from debugging elusive output issues later. It also ensures consistency across your codebase, making it easier for other developers to understand file boundaries and preventing accidental inclusion of non-executable text.

For those managing large-scale PHP environments or working with frameworks like Laravel, maintaining strict output hygiene is part of best-practice development. While tools exist to strip whitespace automatically during deployment, relying on them adds a layer of abstraction that introduces its own potential points of failure. Writing clean code from the start reduces technical debt and improves the reliability of your API endpoints.

Our network