16

In a situation where little snippets of PHP are used in the html a lot, like Wordpress, can you use PHP inside PHP echos?

Example:

<?php 
    echo "<?php the_author_meta('description'); ?>";
?>

As unnecessary as that may be, can it even be done? If not, one aspect of PHP that still seems to confuse me slightly is how to end and restart PHP when outputting HTML.

Case in point, Chris' answer here: How can I echo HTML in PHP? - I want so badly to put a ?> at the end of his example, but that causes errors. Can someone point me in the direction of some comprehensive info of how this starting/stopping with PHP works when blending with HTML, HTML that itself may use PHP snippets in it.

Community
  • 1
  • 1
user1729506
  • 975
  • 4
  • 15
  • 28

4 Answers4

10

Try:

<?php
    echo htmlspecialchars("<?php the_author_meta('description'); ?>");
?>
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
  • 1
    +1 This is the most appropriate answer, if you already gave the php code in a variable, and you want to "echo" the contents into a webpage. – ssh Apr 29 '16 at 01:19
  • if you are using `$var` variables you use backward slash to skip the symbol `\$var`. it also works for double quotes `checked="checked"` is `checked=\"checked\"` – Dexter Aug 03 '18 at 05:43
8

You cannot have PHP echo more PHP code to be evaluated because PHP interprets your code in a single pass. If you have, say, <?php echo '<?php echo "hello"; ?>'; ?>, You will literally get the text, <?php echo "hello"; ?> as output, and the interpreter will not touch it.

You can, however, jump in and out of PHP at will:

<?php
echo "I am going to be interpreted by PHP.";
?>
I am not interpreted by PHP.
<?php
echo "But I am again.";
?>

If you think that you need to output PHP code that is itself re-evaluated, there is always a better way to accomplish this. If you give a specific example of what you are trying to accomplish (real-world case), then the folks here on SO would be happy to help.

Andrew
  • 2,084
  • 20
  • 32
  • Ultimately, I never wanted to resort to what I was asking about. It was just a morbid curiosity. I ended up going with the simple approach Chris describes in his voted answer here: http://stackoverflow.com/questions/1100354/how-can-i-echo-html-in-php That approach, however simplistic, escaped me on account of the whole no ending tag thing. I just had to read up more on why that is with PHP. I went ahead and wrote it out, tested it, and I managed a solution that's error free with no PHP inception. Your "jump in and out of PHP at will" comment helped. – user1729506 Mar 05 '13 at 16:29
4

In regards to: "one aspect of PHP that still seems to confuse me slightly is how to end and restart PHP when outputting HTML."

<?php
// Do some wicked cool stuff in PHP here.
?>
<html>
<body>
Output some html here
<?php
//More awesome PHP stuff here.
?>
Back to html
</body>
</html>
<?php
// You can do any final stuff you want to do here.
?>

Or maybe you mean something more like this:

<table>
<?php 
foreach($person as $p){
  echo "<tr>";
  echo "<td>".$p['name']."</td>";
  echo "</tr>";
}
?>
</table>
Matt
  • 5,315
  • 1
  • 30
  • 57
1

Yes, but it's a horrible idea. In this case, you should just write the function. If you want to "link" the multiple occurrences together, create your own function that does this (such as function describe() {the_author_meta('description');})

In general, you need to realise that anything between <?php and the next ?> will be considered a PHP block and parsed by the engine. Anything on in those blocks is left as is. If you're having specific issues, please ask about them specifically ;)

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592