Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you put PHP inside PHP with echo? [closed]

Tags:

html

php

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.

like image 728
user1729506 Avatar asked Mar 05 '13 15:03

user1729506


People also ask

Can we use PHP inside script?

PHP is server based, and Javascript is client based. Anything that is being executed in the browser by definition is too late to run back to the server, and be executed. No way to do that.

Can you echo PHP code?

The PHP echo StatementThe echo statement can be used with or without parentheses: echo or echo() .

Does PHP need closing tag?

The closing tag of a PHP block at the end of a file is optional, and in some cases omitting it is helpful when using include or require, so unwanted whitespace will not occur at the end of files, and you will still be able to add headers to the response later.

Can you use div inside PHP?

The HTML <div> tag is used for defining a section of your document. With the div tag, you can group large sections of HTML elements together and format them with CSS.


3 Answers

Try:

<?php
    echo htmlspecialchars("<?php the_author_meta('description'); ?>");
?>
like image 145
Rohan Kumar Avatar answered Oct 06 '22 15:10

Rohan Kumar


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.

like image 23
Andrew Avatar answered Oct 06 '22 15:10

Andrew


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>
like image 4
Matt Avatar answered Oct 06 '22 16:10

Matt