3

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?

Andy Groom
  • 619
  • 1
  • 7
  • 15
  • 1
    Possible duplicate of [Can HTML be embedded inside PHP "if" statement?](http://stackoverflow.com/questions/722379/can-html-be-embedded-inside-php-if-statement) – swidmann Jul 15 '16 at 07:22

13 Answers13

4

You can do it this way

<?php
if ($requiresSignature===true) {
?>
 <b>some HTML</b>
 <b>some more HTML</b>
<?php
}
?>
Rafael Shkembi
  • 786
  • 6
  • 16
  • I didn't realise you could do that - perfect! – Andy Groom Jul 15 '16 at 07:11
  • @AndyGroom Since you find this to be the correct answer, it'd be Humane to go ahead & mark it as the right solution. There are 2 main reasons for this: 1.) It motivates the Contributor to keep up the good works: thus it is like buying the Contributor a Beer (if you will). 2.) It is an indicator to any visitor that this is the Solution that solved your problem because they'd see the green Check-mark that says: This solved my Problem. However; it is not mandatory - just a show of goodwill - considering that the contributor invested time & mental energy to help out, plus it costs you nothing ;-) – Poiz Jul 15 '16 at 08:42
  • haha you're right but maybe he forgot.. lets wait and see haha :) – Rafael Shkembi Jul 15 '16 at 08:43
1

For this usage, the heredoc or nowdoc functionalities of php are the best options, in my humble opinion.

Heredoc

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;

Nowdoc

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;
}
Xyz
  • 5,955
  • 5
  • 40
  • 58
0

Try this method,

<?php
$Html = "";
if ($requiresSignature===true) {
 $Html .="some HTML";
 $Html .="some more HTML";

 echo $Html;
}
?>
Vinod VT
  • 6,946
  • 11
  • 51
  • 75
0
echo '
    some html
    some html
    some html
';

I think that's what you're looking for

Sieekoo
  • 5
  • 1
0

Try this

<?php
if (true){
  ?>

pure html code here

<?php

else{

?>

pure html code here
dios231
  • 714
  • 1
  • 9
  • 21
0

Try this

<?php
    $Html = '';
    if ($requiresSignature===true) {
        $Html .="some HTML";
        $Html .="some more HTML";
        echo $Html;
    }  
?>
Rajkumar R
  • 1,097
  • 8
  • 14
0

You can use heredoc syntax: See more detail in here

echo <<<"MYHTML"
html
lots of html
MYHTML;
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
0

this work for you

 <?php if( var == true ): ?>

 <p>Your HTML</p> 

 <?php endif; ?>
Fahmi B.
  • 758
  • 2
  • 10
  • 23
0

variables output in the plain HTML, consider doing so like this:

<?= $variable ?>

or

<?php echo $variable; ?>
beerwin
  • 9,813
  • 6
  • 42
  • 57
Surendra Suthar
  • 350
  • 2
  • 11
0

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 } ?>
Neel Thakkar
  • 406
  • 6
  • 14
0

You Don' Need to write echo to each line.

You Should use :

echo '
    some html
    some more html
    some more html
';
Prashanth
  • 993
  • 8
  • 18
0

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;?>
Hamid
  • 118
  • 1
  • 9
0

...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 ?>     
Poiz
  • 7,611
  • 2
  • 15
  • 17