Using close / open tags is not a good solution for two reasons: you cannot print PHP variables in plain HTML, and this makes your code very difficult to read (the next block of code starts with an end bracket } , but the reader has no idea has what it was before).
Better use heredoc syntax. This is the same concept as in other languages ββ(e.g. bash).
<?php if ($condition) { echo <<< END_OF_TEXT <b>lots of html</b> <i>$variable</i> lots of text... many lines possible, with any indentation, until the closing delimiter... END_OF_TEXT; } ?>
END_OF_TEXT is your separator (it can be basically any text, such as EOF, EOT). Everything between them is considered a string using PHP, as if it were in double quotes, so you can print variables, but you do not need to avoid quotes, so it is very convenient for printing html attributes.
Please note that the closing delimiter must begin at the beginning of the line, and the semicolon must be placed immediately after it without other characters ( END_OF_TEXT; ).
Heredoc with single-quoted string behavior ( ' ) is called nowdoc . No parsing is done inside nowdoc. You use it the same way as heredoc, only you put the opening delimiter in single quotes - echo <<< 'END_OF_TEXT' .
Marki555 Jul 15 '16 at 8:56 2016-07-15 08:56
source share