Simple PHP echo code not working

Simple PHP echo code not working

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

Stefan Bogdanescu

Founder & Senior Architect

The Silent Syntax Error in Your PHP echo Statement

When a simple PHP script outputs a stray "; ?> at the end of your text, it is almost never a mystery regarding what you intended to write. It is a direct reflection of exactly what the server sent back to the browser. In the specific case where echo "<td>This is working.</td>" results in the literal string "; ?>, the root cause is almost certainly the use of HTML entities within your PHP logic instead of raw strings or properly escaped content.

The source material shows a developer attempting to output an HTML table row containing the text "This is working." inside an <html> document. However, the code snippet provided uses &lt; and &gt;. These are not special characters that the browser will render as brackets or angle quotes; they are the literal character sequences representing less-than and greater-than signs in HTML source code. When you pass these entities into a PHP echo statement without decoding them first, you are telling the server to print the text &lt;td&gt;This is working.&lt;/td&gt;.

While this specific error explains the content issue, it does not explain the trailing semicolon and closing tags appearing in the output. The output log shows:

This is working.";
?>

This indicates that the PHP parser successfully executed the echo command but failed to close the script block correctly before the browser rendered the rest of the HTML, or worse, the entire file was treated as a text file rather than an HTML document because the closing ?> tag inside the logic broke the flow.

The Hidden Culprit: Whitespace and Tags

The most common reason for seeing <?php, ?>, or semicolons in your output is the presence of whitespace or newline characters outside of the PHP tags. In older versions of PHP, this was a critical issue known as "whitespace output buffering." Even if you use echo correctly, if there is a space before the opening <?php tag or immediately after the closing ?> tag, that whitespace gets sent to the browser.

However, in modern PHP (7.4 and later), this behavior has changed significantly for production environments. The display_errors setting or the output buffering settings on the server might be masking where the actual break occurs. But looking at the specific code provided:

&lt;?php
    echo "&lt;td&gt;This is working.&lt;/td&gt;";
?&gt;

There are two distinct problems here. First, as mentioned, the HTML entities must be resolved. If you want to output an actual table cell, you should not escape the brackets in the string unless you specifically intend to print that text as literal characters. Second, and more subtly, the placement of the closing ?> tag matters immensely when mixing PHP with HTML.

If your file is intended to be a standard .php file containing HTML, you must ensure that no whitespace exists after the last line of PHP code before the closing ?>. If you place a newline character or a space after the semicolon in the echo statement but before the closing tag, and if your server configuration is strict about stripping output, it might behave unexpectedly. However, the most likely scenario for seeing "; ?> appended to your text is that the string being echoed actually contains those characters, or the PHP block was terminated prematurely by an error earlier in the file that caused the parser to output raw tokens.

The Correct Approach: Decoding Entities and Managing Output

To fix the immediate display issue where brackets appear as &lt; and &gt;, you need to decode the HTML entities before echoing them, or simply stop using them if you are writing actual HTML tags. If your goal is to build a table row dynamically, the code should look like this:

echo '<td>This is working.</td>';

Notice the lack of &lt; and &gt;. By writing < directly in the PHP string, you are telling PHP to output a less-than sign. The browser then interprets that as the start of an HTML tag.

If you strictly need to output the text "This is working."; ?> as literal characters (which seems unlikely given the context of building a table), you would use htmlentities() on the string first, but usually, you want the opposite: raw tags.

Furthermore, consider the trade-off between using ?> and omitting it entirely. If your entire file consists of HTML with only small blocks of PHP logic, modern best practices suggest removing the closing ?> tag from the end of the file or from inside mixed-content blocks. This prevents accidental whitespace from being printed after the script runs. For example, if you write:

echo "Data"; ?>

</html>

You risk sending a newline character after Data. By using short tags or omitting the closing tag in purely HTML files (using <?= for output-only), you avoid this pitfall. However, since the source material uses standard PHP tags, the fix is primarily about string content.

Why This Matters in Production

In a production environment, such as when deploying on a platform like LaravelCompany.com, these small syntax errors can lead to broken layouts or security vulnerabilities if user input isn't handled correctly. The distinction between printing a variable and echoing an HTML entity chain is fundamental. If you are building dynamic tables, ensure your loop logic outputs the raw HTML structure rather than escaped strings unless you are explicitly sanitizing data for display in a text-only context.

The solution is to verify your source code for accidental character encoding issues and ensure that the string passed to echo matches the intended output exactly. By removing the HTML entity references (&lt;, &gt;) from your PHP strings, you allow the browser to interpret them as structural HTML elements rather than text.

Our network