I am learning PHP (no programming experience) from a book. The examples in the book use a strange way of outputting a large block of HTML conditionally. It closes the PHP tag inside the conditional, and reopens it after outputting the HTML. I understand (after some head scratching) how it works, but it seems like a dodgy, not-intended-to-be-used-like-this, workaround.
<?php
if(something == somethingelse) {
echo "some message";
}
else {
?>
<big-block-of-html>
</big-block-of-html>
<?php }
?>
The book did introduce the heredoc syntax, but never used it. Is there a right way of doing this? It would seem more intuitive to output the HTML from within PHP.
Step 1: Firstly, we have to type the Html code in any text editor or open the existing Html file in the text editor in which we want to use the PHP. Step 2: Now, we have to place the cursor in any tag of the <body> tag where we want to add the code of PHP. And, then we have to type the start and end tag of PHP.
Using echo or print: PHP echo or print can be used to display HTML markup, javascript, text or variables.
PHP multi-line line comment begins with /* , and ends with */ .
The <= tag is called short open tag in PHP. To use the short tags, one must have to enable it from settings in the PHP. ini file. First of all ensure that short tags are not disabled, To check it, go into php.
That's exactly how PHP is supposed to be used and is much more readable, elegant and robust than all alternatives*. I'd just go for a better indented style:
<?php
// normal
// code
// here
?>
<?php if ($foo) : ?>
<div>
<!-- more HTML -->
</div>
<?php endif; ?>
* Unless you go for completely code-free templates like Smarty of course...
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