1

Okay, so I have a question. I use print <<< END followed by an END; after my html and variables instead of echo and print. I use this because it doesn't seem to have an effect on speed and the code just looks more organized in my opinion. I'm sure others will disagree but it's just my opinion.

I have a current project and that's the primary method I use to output HTML. No problems so far.

What are the disadvantages to using this? I have spoken with coders about it before, but they never really give me a reason not to use it just to not use it. I would appreciate any advice on this because I haven't had any problems with it.

Brad45
  • 199
  • 7
  • 1
    Possible duplicate: [Is there a reason to use Heredoc in PHP?](http://stackoverflow.com/questions/5673269/is-there-a-reason-to-use-heredoc-in-php) – Wylie Jul 10 '11 at 00:30
  • @Wylie: No. That question is "what are the advantages". This one is "what are the disadvantages". It's quite clear that they are polar opposite questions! – Lightness Races in Orbit Jul 10 '11 at 00:44
  • 1
    tut, tut, no HTML in pure PHP files! a tiny bit of PHP in HTML files is okay, however. – Aditya M P Jul 10 '11 at 01:04
  • Why is it bad to have html in php files? I see no problems with it. I've had no performance hits, and it works just fine. It sounds to me like it's a pride problem. There's no shame in using HTML in php files. Pure PHP is subjective anyway. – Brad45 Jul 10 '11 at 04:09

2 Answers2

4

The syntax you're describing is called a heredoc. As far as I know there is no performance difference between using heredocs and single- / double-quoted strings.

Heredocs can often help to prevent syntax errors, because there is no need to escape ' and " within the string. Another option would be to jump out of PHP into plain HTML, which requires no echo calls whatsoever:

<?php

... do things ...

?>
<div id="result"><?php var_dump($result); ?></div>
<?php

... do more things ...

?>
Jon Gauthier
  • 25,202
  • 6
  • 63
  • 69
  • 1
    Saying it increases readability is actually a bit subjective since heredoc also destroys indentation. – Mike Jul 10 '11 at 00:36
  • Thank you for giving me the proper name of it. I picked this method up a few years back and just kept using it, and to be honest I didn't know much about it. When I spoke to others about it, they simply just told me to avoid it. So far, I have constructed an entire project using it and haven't had any issues with any of the code. It has actually saved me a lot of time so that I can focus on other things. I just didn't know why some programmers told me to avoid it altogether. – Brad45 Jul 10 '11 at 00:38
  • 1
    I have to disagree that it improves readability... as Mike says it destroys indentation which makes it less readable IMO. – prodigitalson Jul 10 '11 at 00:38
  • Right, I chose the wrong phrasing there — I meant that it increased readability in that the writer / reader of the code doesn't need to concern themselves with escaping quotes nearly as much. I updated the answer to be a bit clearer. – Jon Gauthier Jul 10 '11 at 00:46
  • @Hans: thats true, but if you have something that complex its better to use string manipulation functions in my opinion.. im a big fan of `sprintf` and `strtr` :-) – prodigitalson Jul 10 '11 at 00:48
  • This answer does not talk about disadvantages, which was the question. – Lightness Races in Orbit Jul 10 '11 at 00:49
  • Well readability really only affects myself. I'm the only one working on this project. If you guys want to see the project, it works extremely well, and like Hans Engel said, escaping quotes is easy but it can be time consuming. Avoiding this, has allowed me to put more focus on other things. (http://www.khoolz.com) – Brad45 Jul 10 '11 at 00:51
  • Well, I suppose you're correct Tomalak. I jumped the gun, I apologize. Both answers were stellar, but since you raised the point of actually addressing the question, I will accept yours. – Brad45 Jul 10 '11 at 00:56
2

The only disadvantage i can think of is its harder to read for developers. This too is opinion but i find it much easier to read alternate syntax in template files, i.e.:

<?php if($something): ?>
  <div id="something">
    <?php echo $something->text ?>
  </div>
<?php endif; ?>

And switching in and out like this is the only reason i can see to use heredoc as far as html is concerned. IF you have functions that are outputting massive amounts of html then you should change those to include a file in some manner. IE. you shoudl need to switch in and out of html except in your view, and those views should be separate completely form your functions or models. for exampl you should be doing:

function getSomething($var){
  if($var){
   $html = <<< HTML
<div id="something">
  $var->text
</div>
HTML;
 }
}

This is obvioulsy a simle example and actually this example isnt so bad, but if the HTML is more complex it starts to get jsut ugly. And in the case of methods on model classes its just plain evil no matter how simple the HTML is. Id prefer something like the following:

getSomething($var, $template = 'something.php')
{
   if($var){
     ob_start();
     include($template); // $var is accessible in something.php
     return ob_get_clean();
   }

   return null;
}

Of course the include will result in slight a performance hit but thats where caching comes in :-)

prodigitalson
  • 60,050
  • 10
  • 100
  • 114
  • +1, I forgot about that: You can't evaluate conditional statements within heredocs. Most web apps echo a lot of stuff conditionally, so this is a big disadvantage of putting everything out in heredoc format. – Jon Gauthier Jul 10 '11 at 00:40
  • @Hans: You can't evaluate conditional statements inside a double-quoted string, either. The issue here is that it's ostensibly _messier_ to jump in and out of the string with heredoc than it is with double/single quotes and/or HTML mode. – Lightness Races in Orbit Jul 10 '11 at 00:50