9

I'm rather new to programming and i know how to separate PHP from HTML, but i would like to know if there is any difference in doing

this:

<?php $rand="I love apples" ?>
<h1>This is a title</h1>
<div>
    <p>This is a paragraph</p>
    <?php echo"The variable contains the string $rand"; ?>
</div>
?>

compared to doing this:

<?php
    echo "<h1>This is a title</h1>";
    echo "<div>";
    echo "<p>This is a paragraph</p>";
    echo "The variable contains the string $rand";
    echo "</div>";
?>

Is there any difference between in performance etc, between splitting the PHP code from the HTML code and just echoing the whole page in php?

Kenneth .J
  • 1,433
  • 8
  • 27
  • 49
  • possible duplicate of [What's the best way to separate PHP Code and HTML?](http://stackoverflow.com/questions/62617/whats-the-best-way-to-separate-php-code-and-html) – Jessica Dec 18 '13 at 17:04
  • 4
    one major difference is the sanity of anyone having to look at the code. Maintaining html written through php is a horror so dark it makes Dante cry. Top method please. Only echo html in PHP when it is absolutely necessary. – Kai Qing Dec 18 '13 at 17:06
  • @Jessica: Not really, it's not a duplicate. – Madara's Ghost Dec 18 '13 at 17:06
  • Top method. I absolutely detest code written like your last example - far too difficult to read and to type! – ajtrichards Dec 18 '13 at 17:07
  • Choosing one or the other for performance isn't really the right decision. The reason for the separation is maintainability. – naththedeveloper Dec 18 '13 at 17:07
  • Side note: Your first body of code, is invalid. Remove the last `?>` – Funk Forty Niner Dec 18 '13 at 17:08
  • 1
    @Jessica: I didn't say that. I said that it wasn't a duplicate of the one **you** linked. – Madara's Ghost Dec 18 '13 at 17:09
  • @codehorse - that's not really true. There are some instances where it makes sense to write html out via PHP. Defining html email templates for example. HEREDOC is an alternative but F that. So that would be where you can do both. – Kai Qing Dec 18 '13 at 17:09
  • @JakeGould Why u improve only half of the question :S – Daniel W. Dec 18 '13 at 17:10
  • [Y'all should join into this discussion](http://stackoverflow.com/questions/20648511/php-within-html-etiquette) on a similar note. ♫ – Funk Forty Niner Dec 18 '13 at 17:10
  • @DanFromGermany Because the lack of a space with `echo` in the original question seemed like a mistake more than an intent. My full answer expresses my full feelings on the topic. – Giacomo1968 Dec 18 '13 at 17:14
  • 1
    This question is *NOT* a duplicate of http://stackoverflow.com/questions/62617/whats-the-best-way-to-separate-php-code-and-html. This question here asks about the *why* not the *how*! Nor I can't understand why somebody voted for this question being primary opinion based. There is a clear benefit in why you should do that, no opinion involved at all here, just best practice. – floriank Dec 18 '13 at 17:16

6 Answers6

8

The best practice is not to seperate PHP from HTML, the best practice is to seperate logic from markup.

Also important is coding style. Proper line indentions. Using echo "</div>"; instead of echo"</div>";, valid HTML, not putting variables into quotations:

echo "The variable contains the string $rand";

better (why? see my comment below):

echo "The variable contains the string ",
     $rand,
     " :-)";

Your whole project gains much quality and worthness just by improving the code, writing clean, readable, maintainable. Imagine you want to change the Text, you would have to add or change lots of echoes.

Code Style Guides > Pear, PSR, Zend <

encourage developers to keep their code readable, valid and cross-browser compatible

Daniel W.
  • 31,164
  • 13
  • 93
  • 151
  • or You can do `echo "The variable contains the string {$rand}";` which is also considered as clean solution the this... – shadyyx Dec 18 '13 at 17:11
  • Why not put variables into quotations? – Madara's Ghost Dec 18 '13 at 17:11
  • `{..}` is also a good approach, but very often, specially people who are new to PHP, try to use things like `echo "text $array[123] or $array2[asd] or $arrray4["123123"]";` (which doesn't work) and stuff like that, it's just way cleaner to seperate these parts with a dot or a comma, do you notice how stackoverflow highlights the parts of code differently? – Daniel W. Dec 18 '13 at 17:13
5

The problem is not performance, it's about readability and more importantly, maintainability.

Doing all the processing in one place, and all of the output in another (i.e. Logic and Presentation), would mean you will have an easier time altering one without affecting the other too drastically.

To your specific question, the top method is preferable by far, for the reasons listed above.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
2

Taking your question at face value, there are two reasons that come to mind immediately:

  1. Assuming you're using a smart editor, echoing all your HTML will cause you to lose syntax highlighting for it, so you're less likely to catch errors.
  2. Because everything is inside a PHP string, now you have to worry about escaping all your other special characters. Try spitting out some Javascript with a string in it and let us know how fun that is.

However, when most people say something like "separating PHP from HTML" they are referring to the concept of separating your logic from your views. It means don't put complex business logic, computations, and database calls inside your html pages. Keep that all in pure PHP files, and have your html files contain minimal PHP that's only used to spit out your data.

Tesserex
  • 17,166
  • 5
  • 66
  • 106
1
<?php $rand="I love apples" ?>
<h1>This is a title</h1>
<div>
    <p>This is a paragraph</p>
    <?php echo"The variable contains the string $rand"; ?>
</div>
?>

The above looks poorly separated. This is what php/html separation should look like:

<?php 
$rand="I love apples"; 
?>
<h1>This is a title</h1>
<div>
    <p>This is a paragraph</p>
    <p>The variable contains the string <?=$rand ?></p>
</div>

Performance-wise, that's not an issue but it would do much favor for programmers to be able to read the code easily, hence the need for HTML/PHP separation practices. Ideally, if you're going to do just one script, keep all your PHP code at top. Also, other reason for the separation is that IDE editors can easily format HTML nicely. If there's a HTML tag inside the PHP tag that is ending with a HTML tag outside of PHP, then HTML cannot be formatted correctly. For example:

<div><p>And it offers so much <?php echo "$features</p>
<h2>Proven Ideas";?></h2>
<p>More details ahead</p>  
</div>

The above will run just fine but the IDE html formatter will likely be confused with missing end tags and won't format making it more difficult for programmers to read them.

netrox
  • 5,224
  • 14
  • 46
  • 60
0

I think you example is not a good one that makes it very clear why you should separate it.

The reason why you should separate not just HTML but the presentation, rendering or UI part of your application is clean coding and separation of concerns. This will make sure your get clean, easy to read code and makes your application maintable.

Take Wordpress for example, it is an extremely fugly mix of php and HTML. They even do SQL queries in the presentation layer of the application, if you can even draw a borderline between presentation and other logic in this thing.

You'll always have to output some dynamic content in your HTML but really try to reduce it to echoing variables and having some output formatting helper objects there. All business logic should be somewhere else, just not in the "templates" or whatever else you'll call the files that contain the output.

Have a look at the MVC pattern for example, it gives you a good idea of how and why you want to separate things.

floriank
  • 25,546
  • 9
  • 42
  • 66
0

In my opinion, it depends on the level of HTML formatting that is being done versus PHP logic. Nothing more & nothing less. It’s simply easier to read pure HTML as pure HTML or PHP as straight PHP. When it is all jummbled together—the way some templating systems handle it—it becomes a logical headache to read & debug. So I err on the side of placing HTML in PHP for my own sanity’s sake.

Unclear on the performance pluses or minuses if there are any. But can assure you that in 20+ years I have never had a server slow down because of too much HTML embedded in PHP

Personally, I would format your code example like this:

<?php
    echo "<h1>This is a title</h1>"
       . "<div>"
       . "<p>This is a paragraph</p>"
       . "The variable contains the string $rand"
       . "</div>"
       ;
?>

I like this method since there is one echo—which makes it clear what is happening—and the rest of the HTML is just concatenated via . characters.

Also, remember all formatting in programming benefits HUMANS more than anything. A computer only needs to see the commands, so if you want to be pitch perfect for a machine, just code without any spaces or formatting. Heck, stop using full words & just use 1 letter variables! Oh wait, that is how it was done in ye olden days.

Nowadays compilers & caching systems are designed to take human readable code & make it machine optimized.

Which is all to say: You should code towards readability & logic on your part. Nothing more & nothing less.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103