PHP echo to display image HTML

PHP echo to display image HTML

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

Stefan Bogdanescu

Founder & Senior Architect

Debugging PHP Echo Output: Why Your Image Source Breaks

When you attempt to dynamically serve an image based on a query parameter, encountering raw quote marks or unexpected characters in your browser is a classic symptom of how HTML attributes and string interpolation interact. In the scenario where you are receiving "; ?>' instead of your intended <img> tag, the root cause lies in the mismatch between the data structure of your input and the quoting strategy used to construct your output.

Your current script relies on PHP's implicit string conversion via concatenation with single quotes:

<?php 
$result = $_GET['image'];
echo '&lt;img src="images/gallery/'.$result.'.jpg"&gt;'; 
?>

Here, $_GET['image'] extracts the string value (e.g., image01). When PHP concatenates this variable inside a single-quoted string, it inserts the value directly. However, if your $result variable contains characters that conflict with the surrounding quotes or if the input data itself is malformed, the browser's HTML parser becomes confused.

The specific error output of "; ?>' suggests that the generated HTML structure is invalid. Browsers are resilient; they will attempt to render broken tags by ignoring content between mismatched quotes. If your $result variable contains a single quote (apostrophe) or if the concatenation results in an unclosed double quote within the src attribute, the browser stops parsing the src value at the first valid closing quote it encounters. Everything following that point is treated as text content rather than part of the image source URL.

To resolve this, you must ensure the HTML structure remains syntactically correct regardless of the input value. The most robust approach is to use double quotes for the outer string in PHP and escape any internal single quotes found in the filename, or conversely, switch entirely to single quotes for the attribute values if your filenames might contain characters that complicate double-quote parsing.

Consider this corrected implementation using double quotes for the PHP string and proper escaping:

<?php 
$result = $_GET['image'];
// Escape single quotes within the filename to prevent breaking the HTML structure
$escapedResult = str_replace("'", "\'", $result); 
echo "<img src=\"images/gallery/" . $escapedResult . ".jpg\">"; 
?>

Alternatively, if you anticipate filenames might contain spaces or special characters that require encoding, using htmlspecialchars is often safer for security reasons to prevent XSS attacks, though strictly speaking, image filenames usually do not require HTML entity encoding unless they are user-generated content displayed as text. However, the immediate fix for the broken tag syntax is ensuring the quotes around the src attribute are balanced.

Another critical aspect of this workflow is input validation. Relying solely on $_GET without verifying the input against an allowed list leaves your application vulnerable to open redirects or path traversal attacks if a user inputs something like ../../etc/passwd. Since you are constructing file paths dynamically, you should whitelist valid image filenames before concatenating them into the URL.

If you are building larger applications, consider moving this logic away from raw echo statements and into a dedicated view layer or using a framework like Laravel. Frameworks handle escaping automatically when rendering views, reducing the risk of manual quote mismatches. For instance, in Laravel, you would pass the filename to a Blade template which handles the HTML generation securely:

// In a controller
return view('gallery', ['imageName' => 'image01']);

// In the Blade template
<img src="{{ asset('images/gallery/' . $imageName . '.jpg') }}" alt="Gallery Image">

This separation of concerns ensures that the HTML is generated safely without worrying about quote escaping in your PHP logic. The key takeaway is to treat the filename as data, not code, and ensure your string delimiters never conflict with that data.

Our network