0

Im trying to have php code that contains an echo. within that echo however, I need it to echo php code within it. a short example is bellow.

<!php echo '<?php echo "test"; ?>'; ?>

My main reason for this is that I will be using a mysql_fetch_array while loop that echoes out some html and $row[] information in it but also needs php in it. Bellow I have put what I actually need to be done.

while ($row = mysql_fetch_array($result)) {
echo '<div style="position: relative;" >
<img src="Backgrounds/'. $row[id] .'.jpg" alt="error">
<br /><br />
<?php
Some php content that also uses $row[] content
?>
<br />
</div>';
}

My only problem is that when I inspect element in chrome, it turns out like this

while ($row = mysql_fetch_array($result)) {
echo '<div style="position: relative;" >
<img src="Backgrounds/'. $row[id] .'.jpg" alt="error">
<br /><br />
<!--?php
Some php content that also uses $row[] content
?-->
<br />
</div>';
}

and it turns the php part into comments for html. Im not sure what to do here.

6
  • 2
    Stop using the deprecated and as of PHP7 removed mysql_* functions. Migrate to PDO and start using Prepared Statements. Commented Mar 26, 2016 at 1:16
  • Why do you want php code to be executed? eval is probably what you want. php.net/eval Commented Mar 26, 2016 at 1:17
  • @CharlotteDunois "NoOne" really wants evil in this context ;) Commented Mar 26, 2016 at 1:19
  • @CharlotteDunois I think he wants to print out php examples rather than execute php code. This was just my interpretation though. Commented Mar 26, 2016 at 1:20
  • @Rawr Poorly described then. Commented Mar 26, 2016 at 1:21

2 Answers 2

2

In a html context, <?php, <something, etc, is the opening of a html tag. Edit < with his html entity &lt; to prevent it happening..

&lt;?php

Even better, use htmlspecialchars:

<?php echo htmlspecialchars('<?php echo "test"; ?>'); ?>
Sign up to request clarification or add additional context in comments.

3 Comments

do i do the same with the end also? like ?&lt;
The lt in &lt; stands for less than which is the < symbol. If you want the greater than sign then you'd use &gt;
the &lt; and &gt; symbols for some reason put it into quotes and then displays all the inside php on the screen while the htmlspecialcharacters outputs all the code inside it on the screen
0

Otherwise, try reversing it:

while ($row = mysql_fetch_array($result)) 
{
    ?>
    <div style="position: relative;" >
        <img src="Backgrounds/<?=$row[id]?>.jpg" alt="error">
        <br /><br />
        <?php
            //Some php content that also uses $row[] content
        ?>
        <br />
    </div>
    <?php
}

And also switch to PDO objects.

1 Comment

thanks. this works for what I want. I will work on the PDO stuff later.

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.