Can you put PHP inside PHP with an echo?

In a situation where small PHP fragments are used in html a lot, for example Wordpress, can you use PHP in PHP echos?

Example:

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

How is it not necessary, can it be done? If not, one aspect of PHP that still confuses me a bit is how to end and restart PHP in HTML output.

In this case, Chris's answer is here: How can I echo HTML in PHP? β€œIs it so hard for me to put it ?> At the end, for example, but it causes errors. Can someone point me towards some complete information about how this start / stop with PHP works when mixing with HTML, HTML, which itself can use PHP fragments in it.

+8
html php
source share
4 answers

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

You can, however, enter and exit PHP as desired:

 <?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 you need to output PHP code that is being reviewed, there is always a better way to accomplish this. If you give a concrete example of what you are trying to accomplish (the real thing), then people here at SO will be happy to help.

+7
source share

Try:

 <?php echo htmlspecialchars("<?php the_author_meta('description'); ?>"); ?> 
+3
source share

Regarding: "One aspect of PHP that still confuses me a bit is how to end and restart PHP when releasing 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:

 <table> <?php foreach($person as $p){ echo "<tr>"; echo "<td>".$p['name']."</td>"; echo "</tr>"; } ?> </table> 
+2
source share

Yes, but this is a terrible idea. In this case, you should just write a function. If you want to β€œlink” multiple occurrences together, create your own function that does this (for example, function describe() {the_author_meta('description');} )

In general, you need to understand that anything between <?php and next ?> Will be considered a PHP block and parsed by the engine. Everything that is included in these blocks remains as it is. If you have special problems, please ask about them specifically;)

+1
source share

All Articles