6

Possible Duplicate:
Easiest way to echo HTML in PHP?

Hello,

one simple and short question. If you have a php file that contains HTML code, is it better to do the output with echo or to write the HTML code directly into the file? For example the file some.php contains either:

<div>This is a text</div><a href="www.example.com">test</a><?php if(---) { whatever; } ?>

or:

<?php echo "<div>This is a text</div><a href=\"www.example.com\">test</a>"; if(---) { whatever; } ?>

Which version is faster, cleaner and better?

Community
  • 1
  • 1
phpheini
  • 399
  • 2
  • 4
  • 10
  • Might be more suitable on programmers.SE...? – Zsub Mar 01 '11 at 15:16
  • I think this question is a little bit subjective. Both options would do the same, but it depends on your situation and your preferences which one you would choose. – Michiel Pater Mar 01 '11 at 15:19
  • 1
    This is one thing that really upsets me; why, oh why do mash all of our web-code together which is reminiscent of that main component of many an Italian dish. – Grant Thomas Mar 01 '11 at 15:20
  • I don't think it's subjective at all - why is web development so different? It seems we strive to make the largest messes of amalgamations of different languages in a single file that we possibly can. I don't get it, man. Encapsulation! Separation! Maintainability! – Grant Thomas Mar 01 '11 at 15:21
  • 3
    Duplicates: [Easiest way to echo HTML in PHP](http://stackoverflow.com/questions/1100354/easiest-way-to-echo-html-in-php), [How to echo in PHP, html tags](http://stackoverflow.com/questions/3931351/how-to-echo-in-php-html-tags), [Escape HTML to PHP or use Echo](http://stackoverflow.com/questions/505642/escape-html-to-php-or-use-echo-which-is-better)... – ircmaxell Mar 01 '11 at 15:25
  • @Mr. Disappointment - Try and remember that not everything needs to be a full blown web-app with MVC and "proper standards". You have zero context for the questioner's situation and for all you know he is using proper design patterns but just didn't feel like explaining his entire technology stack in a simple SO question. – Jarrod Nettles Mar 01 '11 at 15:42
  • @Jarrod: Very observant, however, my comments pertain to principles (or the lack of following such) which can simply be applied in any related scenario, tiny or huge scale - that's the beautiful thing about principles, they tend to stick regardless of scope. I could use some examples of reducing to the absurd which amplify points, but, TBH, I have to take such a childish tact at work and wish not to _beat a dead horse_ here. – Grant Thomas Mar 01 '11 at 15:46

4 Answers4

6

In my opinion this really depends on how long the code snippet is. For simple lines I'd just use echo. For longer parts (especially with lots of " to escape) i'd prefer "closing" the php code (your second approach).

Depending on the amount of text/HTML to print I'd also consider using some kind of simple templating engine to load a template file and just fill in dynamic variables/placeholders/gaps.

Mario
  • 35,726
  • 5
  • 62
  • 78
  • 1
    The thing is: as soon as you have for example an if-clause I dont think it is better to do like: if(something) { ?> hello This is rather a really bad way, isnt it? – phpheini Mar 01 '11 at 15:37
  • @phpheini not THAT bad. it's really up to you. However you're missing a REAL point: as soon as you have to echo anything in the middle of business logic, you know you're doing something wrong. Learn templates, dude. Learn templates. – Your Common Sense Mar 01 '11 at 17:15
  • What do you mean by in the middle of business logic? – phpheini Mar 01 '11 at 19:14
  • Do some calculations. Print HTML. Do some more calculations. Print HTML. - Where HTML is more than 1-2 simple lines. – Mario Mar 02 '11 at 12:45
5

I prefer the first method meaning HTML shouldn't be mixed with PHP as much as possible. With this I don't have to worry about mis-match of quotes, etc. There exists heredocs syntax but yet the first method is something I find easier to work with.

Just choose a way you are more comfortable with and that makes it easier for other programmers to handle you code if they have to :)

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
4

Your question seems to imply that you keep your PHP and HTML in the same file. You might want to think about separating the two. This would leave you with an HTML template that would look like your first example.

Not only will this make it easier to focus on either PHP or HTML, but like lonesomeday mentioned you will be able to leverage your IDE's HTML handling capabilities which can make things a lot easier for you. Plus it's easier to let web designers work on the frontend without them having to understand PHP.

Manos Dilaverakis
  • 5,849
  • 4
  • 31
  • 57
-1

Please rewrite:

<?php 
echo '<div>This is a text</div><a href="www.example.com">test</a>'; 
if(---) { whatever; } 
?>

The single quote parses faster then the double quotes. Second, I have heard that the opening and closing of PHP blocks would be a performance penalty, but I can't confirm that.

It is just a personal preference. I like it to echo all the HTML with PHP.

Fokko Driesprong
  • 2,075
  • 19
  • 31
  • 2
    No, it's not a personal preference, it's about wanting to use some design patterns or not, which you obviously don't care about :-) – Capsule Mar 01 '11 at 15:23