At the moment my PHP code is like this:
<?php
some code
?>
lots of HTML
<?php
some more code
?>
I now want to include large chunks of HTML depending upon the values of certain PHP variables so like this:
<?php
if ($requiresSignature===true) {
echo "some HTML";
echo "some more HTML";
}
?>
Using echo
is fine for a few lines of HTML but is there an easier way when I've got maybe 500 lines of HTML so I don't have to type echo
in front of each line?
Try this method,
<?php
$Html = "";
if ($requiresSignature===true) {
$Html .="some HTML";
$Html .="some more HTML";
echo $Html;
}
?>
echo '
some html
some html
some html
';
I think that's what you're looking for
You can do it this way
<?php
if ($requiresSignature===true) {
?>
<b>some HTML</b>
<b>some more HTML</b>
<?php
}
?>
Try this
<?php
if (true){
?>
pure html code here
<?php
else{
?>
pure html code here
Try this
<?php
$Html = '';
if ($requiresSignature===true) {
$Html .="some HTML";
$Html .="some more HTML";
echo $Html;
}
?>
You can use heredoc syntax: See more detail in here
echo <<<"MYHTML"
html
lots of html
MYHTML;
this work for you
<?php if( var == true ): ?>
<p>Your HTML</p>
<?php endif; ?>
variables output in the plain HTML, consider doing so like this:
<?= $variable ?>
or
<?php echo $variable; ?>
It's useful, try this code:
<?php
if ($requiresSignature===true) {
$var = "some HTML";
$var .= "some more HTML";
echo $var;
}
?>
**OR**
<?php if ($requiresSignature===true) { ?>
HTML CODE
<?php } ?>
You Don' Need to write echo to each line.
You Should use :
echo '
some html
some more html
some more html
';
PHP is a HTML embed language.You can try it every where of your php page.
<?php if ($requiresSignature===true): ?>
<p>"some more HTML"</p>
<?php endif;?>
...but is there an easier way when I've got maybe 500 lines of HTML so I don't have to type echo in front of each line? It turns out there is, so yes and here is how:
<?php
$myString = "PHP is so cool...";
if($requiredSignature === true):
?>
<div class='too-much-html-markup'>
All the RAW HTML MARKUP here would only be displayed if (and only if)
the condition above evaluates to true so i don't have to worry about
any kind of echoing. However, i can still go ahead and echo some
content here if i still choose like this: <?php echo $myString; ?>
And everything will still work out just fine.
</div>
<?php else: ?>
<div class='still-some-raw-html-in-else-clause'>
Again this is a raw Markup and will only be rendered if the IF
condition above evaluates to FALSE!
</div>
<?php endif; //<== NOW I END THE CONDITIONAL LOGIC WITH endif KEYWORD ?>
For this usage, the heredoc or nowdoc functionalities of php are the best options, in my humble opinion.
Heredoc
is like echo "Foo bar";
but intended for a large chunk of text, spanning multiple lines.
Like this:
echo <<<FOO
<h1>Foo bar</h1>
<p>Lorem ipsum dolor sit tenet conseqteur...</p>
<i>Created by $name</i>
FOO;
This syntax is also available for setting variables, class properties, class constants and static variables (since php 5.3). The FOO
part, you can set yourself. Just remember to close the Heredoc
with the same ending on a line by itself (with absolutely no indentation), ended with a semicolon.
E.g.
$foo = <<<BAR
This is an example text.
Spanning multiple lines.
BAR;
Think of Nowdoc as the '
equivalent of "
. That is, no variable substitution is performed inside a Nowdoc statement, just like none is performed inside a 'single quoted string'
.
The syntax is like this:
echo <<<'EXAMPLE'
This
is
a
test
EXAMPLE;
In conclusion I would do like this:
if ($requiresSignature===true) {
echo <<<HTML
Some html<br/>
And even more <b class="html">html</b>
HTML;
}