2

I'm not very experienced at php, so if there's an easy function for this, I'll feel like an idiot.

When coding in PHP, at the point where you need to echo some HTML code, I have found I have either one of two options.

A: echo "<!--html text here-->";

B: echo "\t\t\t<!--html text here-->\n";

If I were to use method A throughout the code, looking at the php code from client side using view-source produces a solid block of code which is difficult to read.

If I were to use method B, it looks fine client-side, but the actual source code looks messy.

Is there anyway to keep both server and client-side appearance clean?

Faridzs
  • 672
  • 8
  • 23
echolocation
  • 1,120
  • 1
  • 10
  • 28

5 Answers5

2

Something like this

<?php 
//php code here
?>

//html code
<h3> <?= $justADynamicVariable ?> </h3>
<?php
//continue php code
 ?>
Abhishek Kannan
  • 988
  • 8
  • 16
  • 1
    i'd like to add that if, else... can span across multiple sections of php code so in this answer you can start an if(){ in the first php code section and end it with a } in the second – Dany Khalife Jun 28 '13 at 16:05
  • Yes you can follow the same format. Start an *if condition* in one part of your php code and end it with the } brace in another part as required. You will have to indent it properly for easy readability – Abhishek Kannan Jun 28 '13 at 16:06
  • -1: You put a PHP comment outside a PHP block, this is confusing. – Glitch Desire Jul 07 '13 at 22:38
1

Un-readable front end code is caused by poor factoring of your PHP code

The wall-of-text issue occurs because your code is messy. You have logic and display code in the same place (evident from the fact that you're echoing HTML from a PHP block) and this will always produce unreadable and ugly code.

Look at Model-View-Controller pattern, and separate out your logic code from your display code. Then, write your display code in a primarily HTML format with some in-line PHP:

<div>
    Welcome back <?= $this->username; ?>
</div>

If you're having to echo HTML code from a PHP block, your code is probably factored wrong.

Other useful tricks to produce readable code:

Use alternative PHP control blocks

This:

<div id='somediv'>
<?php if($something): ?>
    Some stuff
<?php endif; ?>
</div>

is infinitely more readable than this:

<div id='somediv'>
<?php if($something) { ?>
    Some stuff
<?php } ?>
</div>

And it's definitely better than this which is probably what you're using now:

<?php
    echo "<div id='somediv'>";
    if($something) {
        echo "Some stuff";
    }
    echo "</div>";
?>
Glitch Desire
  • 14,632
  • 7
  • 43
  • 55
0

You can just close the php tag (?>), write the html block and return to php (

Salustiano Muniz
  • 322
  • 5
  • 20
0

There are lot's of ways to produce html, depending or it's a single line or an entire block of HTML.

The important thing is readability.

echo "<div><p>Hi ".$name."</p></div>";

Isn't unreadable persé, it might be annoying because it won't get highlighted in many IDE's. This is a good reason to always seperate html and php, meaning always put html parts outside of your php tags.

So you could end your php block before and open it again after :

if(TRUE) {
?>
    <div><p>Hi <?php echo $name;?></p></div>
<?php 
}

If you have PHP within a large file mostly containing html you are better of using php control blocks:

<!--html text here-->
<?php if(TRUE):?>
    <div>
        <p>Hello <?php echo $name;?></p>
    </div>
</php endif;?>
<!--html text here-->

You could also use heredoc

echo <<<EOD
        <div>
            <p>This is html</p>
        </div>
EOD;

Or output buffering:

ob_start();
?>
    <div>
        <p>Hello <?php echo $name;?></p>
    </div>
<?php
$html = ob_get_clean();

echo $html;
Timmetje
  • 7,641
  • 18
  • 36
0

Use a templating engine, like Twig http://twig.sensiolabs.org/ it allows you to separate your logic out of your templates so you can write really easy to markup with simple syntax to drop in dynamic variables.

or

Use a browser like Chrome, which will auto indent your source for you when you use the Web Developer Tools http://discover-devtools.codeschool.com/. You can even copy paste the results really quickly to a new file. In general, it's a bad idea to fret over outputting well spaced html since software can clean it up so easily.

Mark Fox
  • 8,694
  • 9
  • 53
  • 75