How to Echo an iFrame in PHP

Trying to figure out how to echo an iframe in your PHP code? Here are the three ways to get it done, with code samples.

Published Categorized as PHP

If you’re trying to echo an <iframe> element in PHP but it isn’t working, we have good news for you: it’s an easy fix. It’s probably because you’re using the same type of quotes for the attributes of the <iframe> that you’re also using for enclosing your PHP strings.

In other words, this:

<?php
    echo '<iframe src='https://example.com/'></iframe>';
?>

Or this:

<?php
    echo "<iframe src="https://example.com/"></iframe>";
?>

Will throw an error because the PHP interpreter — the engine that’s parsing your code one line at a time and executing it — will think that you’re closing the string to be echoed when you’re really opening an attribute of the <iframe> element.

Echoing an iFrame in PHP

There are three ways to work around this, and they can generally be used interchangeably. If you want to find out what they are, then read on; I’ll show you how this is done below.

Use Different Quotes

The simplest way to echo an iframe in PHP, and the one that most of you who are reading this post will go for, is to use a different character for the quotes than the one you’re already using to enclose the string for echoing the <iframe> element.

This tells the PHP interpreter, which reads and executes the PHP code one line at a time, that the quotes for the string are not the same as the quotes for the attribute, and so it will treat them differently and won’t throw an error.

For example, if you’re enclosing the string in single quotes ('), then use double quotes (") for the <iframe> element’s attributes:

<?php
    // Use double quotes inside single quotes
    echo '<iframe src="https://example.com/"></iframe>';
?>

And if you’re enclosing the string in double quotes (“), then use single quotes (‘) for the attributes:

<?php
    // Use single quotes inside double quotes
    echo "<iframe src='https://example.com/'></iframe>";
?>

Escape the Quotes

If your team’s style guide requires you to use only single or double codes, then another way to echo an <iframe> in PHP is to escape the quotes inside the string by prepending them with the backward slash (\) character.

<?php
    // Escape double quotes within double quotes
    echo "<iframe src=\"https://example.com/\"></iframe>";
?>

Add the iFrame Between PHP Tags

When the PHP interpreter parses your code, it will only interpret the code that’s between <?php> tags, and not the code between them. So another way to output an <iframe> element in your PHP code is to temporarily close the PHP tags and then add it between them.

<?php
    // PHP code starts
?>
    <!-- HTML code goes here -->
    <iframe src="https://example.com/"></iframe>
<?php
    // PHP code continues
?>

By Dim Nikov

Content creator with marketing and web development skills to pay the bills.

Leave a comment

Your email address will not be published. Required fields are marked *