Nesting PHP Code Blocks

You don’t! At least not like the title suggests. Here is what I wanted to do (nested PHP in bold):

<?php echo "<a href='episode.php?post_ID=<?php echo $prev_ID ?>'>previous</a>" ?>

When I did this, my code would break because the first ?> tag closed my first <?php tag instead of the second. A quick post to Stack Overflow helped me understand what I was doing wrong. Instead of nesting PHP code blocks, concatenate variables. Here is the code using concatenation:

<?php echo "<a href='episode.php?post_ID=".$prev_ID."'>previous</a>" ?>

In this rewrite there is no nesting and no broken code :)

Leave a Reply

Wordpress theme JaeDubya © 2018 | All Right Reserved | Designed & coded by J. Arthur Wetenkamp