3

Hello I looked around a bit for a solution but couldn't find someone with the exact problem as me. Basically I have a php variable that is a url. I want to add a new iframe with that url.

This is what I have, but because HTML is using the double quotes after src the php variable is ignored.

<iframe width="420" height="345"src= "<?php $output ?>"> </iframe>

Thank you guys

1
  • Answers below (you forgot the echo), but just saying that HTML can also use single quotes. Commented Mar 10, 2014 at 19:37

2 Answers 2

5
<iframe width="420" height="345" src="<?php echo htmlspecialchars($output); ?>"> </iframe>

You're missing echo. Also, always use htmlspecialchars() to ensure you are creating valid HTML that isn't vulnerable to injection. If you find yourself doing this a lot, consider using a template engine.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you very much! Worked wonderfully. Most of the work that I have done in the past has all been offline games and such. Haven't done much with online work. I was unfamiliar with htmlspecialchars. What exactly does that do?
@Brad Since PHP 5.4, <?php echo can be safely replaced with <?=. Previous versions required short_open_tag or asp_tags to be on; the former caused problems with XML, but with the latter you could use <%= instead.
@user3369289 It will take characters like < and > and convert them to &lt; and &gt; to make sure they aren't interpreted as HTML. You don't want text content to leak to your HTML. In worse cases, if you are outputting text from users, it might contain <script> tags.
Interesting I will look into this! Thanks again
0
<iframe width="420" height="345" src="<?php echo htmlspecialchars($output); ?>"> </iframe>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.