9

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ntc
  • 257
  • 1
  • 4
  • 9
  • 2
    Yup, is valid. There are lots of similar question, like this http://stackoverflow.com/questions/2788891/strange-php-syntax and this http://stackoverflow.com/questions/2333779/whats-this-kind-of-syntax-in-php and ...http://stackoverflow.com/search?q=php+syntax – ajreal Nov 25 '10 at 12:08
  • 2
    It's worth noting that there're many ways to print HTML (an interesting problem by itself) but closing PHP mode from within a conditional is a well-known and widespread syntactic feature, not a workaround at all. – Álvaro González Nov 25 '10 at 12:11
  • 2
    BTW, this may be easier to comprehend if you think about this the other way around: PHP started out as and is mainly a way to *embed a scripting language into HTML.* Only in your case there's more PHP code than HTML, so the balance is reversed. – deceze Nov 25 '10 at 14:45

4 Answers4

14

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...

deceze
  • 510,633
  • 85
  • 743
  • 889
1

Think about hide this block in other file. Then you can create some function like this:

function get_some_big_block_content()
{
    return get_file_contents('./your_big_block.html');
}

Then you can:

<?php 
    if(something == somethingelse) { 
        echo "some message";
    } 
     else { 
        echo get_some_big_block_content();
    }

?> 
Svisstack
  • 16,203
  • 6
  • 66
  • 100
  • 1
    should be [file_get_contents](http://php.net/manual/en/function.file-get-contents.php) – mfink May 24 '18 at 15:16
0

PHP allows multiple ways of doing this; picking one is mostly a matter of preference - for some, this is the most readable option, for some it's a horrible hack.

In all, inline HTML is hard to maintain in any form - if you're putting any serious effort into your website, consider some sort of templating system (e.g. Smarty) and/or framework (e.g. Symfony), otherwise you'll go mad from trying to maintain the PHP+HTML soup.

Piskvor left the building
  • 91,498
  • 46
  • 177
  • 222
  • Thanks. I am at a relatively early learning stage presently. I intend to learn a framework, but I want to get to know the basics first. – ntc Nov 25 '10 at 12:10
  • 1
    @nethy canning: Good point. Be careful though, HTML soup is addictive and hard to unlearn; read about separating form and content, e.g. this: http://en.wikipedia.org/wiki/Separation_of_presentation_and_content – Piskvor left the building Nov 25 '10 at 12:14
  • 2
    I think I'm learning php/programming at a time when most of the experienced programmers have recently gotten very fed up with (as you call it) soup. It seems like every resource refuses to let me get started before they add a disclaimer about the dangers of "soup" and some helpful words about frameworks, MVC structure, and some favorite recommendations. I found myself reading comparisons of codeigniter vs cakephp before I knew how to declare a variable. Consider me well warned :) – ntc Nov 25 '10 at 12:22
  • @nethy canning: `$warned['nethy canning'] = true;` :) You have a point, though - it took me some time to understand MVC and whatnot. – Piskvor left the building Nov 25 '10 at 12:54
-1

Use

<?php
    if (something == somethingelse) {
        echo "some message";
    }
    else {
        echo "<big-block-of-html>
        </big-block-of-html>";
    }
?>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
benhowdle89
  • 36,900
  • 69
  • 202
  • 331
  • isn't this a problem when you use double quotes inside your html or content? IE: " Someone on stackoverflow said "You can use double quotes" " – ntc Nov 25 '10 at 12:16
  • 1
    @ben Just break out of PHP. :) – deceze Nov 25 '10 at 12:19
  • That's Ok in this case, but what if I want to cut/paste some existing block of html code? I guess that's why the author is closing the php tag in the first place. – ntc Nov 25 '10 at 12:27
  • 1
    That's what HEREDOCs are for: spitting out big chunks of text without having to worry about escaping quotes of any kind and a bonus of getting variable interpolation thrown (basically it's a multi-line double-quote echo). – Marc B Nov 25 '10 at 12:32
  • @Marc Heredoc seemed to me, from the little I know, to be the intended tool for this use, but I haven't seen it used in the (few) examples I've been going over. – ntc Nov 25 '10 at 12:35
  • @Marc Two things I dislike about HEREDOCs: a) usually screws with syntax highlighting, b) often messes up the consistency of my indention since the final closing token has to start on the first character. I find them useful sometimes, but not often. – deceze Nov 25 '10 at 12:39
  • @deceze. Hmm. vim doesn't have any trouble highlighting variables inside a heredoc. It won't colorize html, since it's treating the heredoc's contents as plain text, but variables come through quite nicely. – Marc B Nov 26 '10 at 02:38
  • @Marc My point exactly. IMO HTML highlighting is important for large blocks if it. – deceze Nov 26 '10 at 02:41