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.
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.
The PHP echo StatementThe echo statement can be used with or without parentheses: echo or echo() .
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.
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.
Try:
<?php
echo htmlspecialchars("<?php the_author_meta('description'); ?>");
?>
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With