tags:

views:

108

answers:

3

I asked another question about HTML and PHP separation as I have seen references to it on tutorial and examples pages but I found that separation of HTML and PHP it something different than what people are actually commenting on and I think that is PHP modes.

That is breaking out of PHP or not. Should It be done? Is it important?

Is it just a matter of preference to echo my HTML output or should I break out to display it?

Example:

<?php

echo '<html'>;

?>

vs.

<?php

dostuff();

?>

<html>

<?

morestuff();

?>
A: 

If by breaking out you mean this sort of thing:

<?php
     if($somecondition) {
?>
<!-- Some HTML -->
<?php
     } 
?>

Then yes, breaking out is better in most cases as it is more readable (many IDES highlight HTML syntax, and cannot do so if it is withing a string when being echo() ed)

Macha
Just thought I'd add, normally I do this by typing the alternate form of if: <?php if($someCondition): ?><!-- Some HTML --><? php endif; ?>Just because across blocks, sometimes it's hard to match the brace.
Ray Hidayat
+1  A: 

I assume by "breaking out" you mean:

<?php foo(); ?>
test
<?php bar(); ?>

as opposed to

<?php
    foo();
    echo("test");
    bar();
?>

Well, one advantage of the first solution is that your layout is still more or less readable in HTML editors. Also, it separates layout and logic, at least more than the other variant. It is probably also slightly faster than the second option because no strings need to be parsed and echo'ed. On the flipside, having tons and tons of individual PHP-blocks can really be hard to read because things that are semantically related are suddenly split. You can, of course, also combine both approaches.

I think the bottom line here is that as soon as you need to do so much formatting and echo'ing that the logic of your program becomes really obscured, you should consider using a 'real' template engine.

n3rd
Thanks! By combing approaches do you just mean using each where most appropriate for myself... If it just one echo use it... If its a complex HTML block break out... If breaking out makes it hard for me read the code then don't. That sort of thing?
ian
Premature optimization is the root of all evil. The difference is parsing speed is neglectable, the context switching is quite expensive. so 'faster' depends on the amount of 'text' you want to output. You should instead focus on readability/maintainability.
Jacco
PHP is a great template engine on itself. Just be hard on yourself and only do if,else,for and foreach in your teamplate files.
Pim Jager
That's why I used the inverted commas ;-)
n3rd
A: 

I think it depends on the situation.

how many lines do you want to echo to the browser? do the lines contain $variable values? $array values? do you loop trough a dataset? etc etc.

To me, it is more reable to just echo the lines most of the time.

<?php
   if ( check($something) ) {
      echo "Some variable is: $something<br/>\n";
   } else {
      echo "Some variable is something else!<br/>\n";
   } 
?>

can be more readable than:

<?php
   if ( check($something) ) {
?>
Some variable is: <?php echo $something; ?><br/>
<?php
   } else {
?>
Some variable is something else!<br/>
<?php
   } 
?>

and with some IDEs (or stackoverflow.com syntaxhighlighting for example), it can even be more readable to use:

<?php
   if ( check($something) ) {
      echo 'Some variable is: '.$something."<br/>\n";
   } else {
      echo "Some variable is something else!<br/>\n";
   } 
?>

In summary:
PHP offers you a lot of options to send content to your client.
The 'best method' differs from case tot case.

Choose the method that is most readable/maintainable and use it consistently.

Jacco
Why is this downvoted?
Jacco