PHP echo image to the browser

To get an image displayed on the browser using PHP, you can echo an <img> tag as follows:

echo '<img src="my-picture.png" alt="My image" />';

You need to replace my-picture.png with your actual picture name.

If the picture is not located in the same path as the PHP file, you need to write the right path.

Keep in mind that you need to use a different quote symbol then the one you use in the echo statement.

If you use single quotes for the echo statement, then use double quotes to avoid unexpected identifier error.

See the example below:

echo '<img src="my-picture.png" alt="My image" />';
// or
echo "<img src='my-picture.png' alt='My image' />";

You need to choose one of the two styles above and make it consistent on all your PHP files.

And that’s how you echo an image using PHP 😉

Take your skills to the next level ⚡️

I'm sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.