5
\$\begingroup\$

I'm using Codeigniter but this goes for any project in PHP. Let's say I have the following code in my view. I've been struggling, trying to figure out how best to indent and display PHP code within standard HTML code.

...
...
<div id="subcontent">
    <?php
        if ($this->session->flashdata('message')) {
            ...
            ...
    ?>
        <div class="blah blah">
            ...
            ...
        </div>
    <?php 
        } 
    ?>
</div>

So notice how I nested HTML within the PHP code by escaping w/ the PHP syntax. This can get out of hand if you have lines and lines of code. I'm using Komodo and I've enabled "Indentation Guidelines" but even with that, most of my code doesn't line up. So my question is, how best to handle this?

I've actually resorted to keeping the PHP marker <?php ?> to the far left and not indenting them. Seems to work so far ...

\$\endgroup\$
5
\$\begingroup\$

You can use other kind of syntax:

...
...
<div id="subcontent">
    <?php if ($this->session->flashdata('message')): ?>
        <div class="blah blah">
            ...
        ...
        </div>
    <?php endif; ?>
</div>

So, <?php ?> can be in 1 line and I think it's beauty:)

Anyway, There are no any 'right' way to do it. You can(and should) choose your own one

\$\endgroup\$
2
\$\begingroup\$

This is really an individual opinion..

Personally I'm all for putting the <?php ?> markers at the far left because they indicate different code 'segments'.. There is just no 'best way to do it.. Just choose a way and stick to it..

\$\endgroup\$
  • \$\begingroup\$ Yea, i actually came across this by accident. I had indented all my code to the far left to "reset" everything. I forgot to indent the <?php part and viola I said to myself, "woah that doesn't look too bad." \$\endgroup\$ – luckytaxi Jul 16 '11 at 16:20
  • \$\begingroup\$ Just making sure you know this syntax as wel, but if you just want to include a single PHP value inside a HTML line you can best use <?= $val ?>.. \$\endgroup\$ – Paul Jul 16 '11 at 16:34
  • 1
    \$\begingroup\$ i removed the "short" syntax (whatever it's called) due to JS compatibility. \$\endgroup\$ – luckytaxi Jul 16 '11 at 16:47
0
\$\begingroup\$

I prefer opening PHP scripts with the same indentation as their parent, and then having the actual code and the subsequent tags be indented.

<div>
<?
    $query = mysql_query("SELECT * FROM `table`");
    while($row = mysql_fetch_assoc($query)):
?>
    <div><?=$row['content']?></div>
<? 
    endwhile;
?>
</div>
\$\endgroup\$
0
\$\begingroup\$

That's a question about personal preferences. I always work with templates (smarty) to seperate views from logic.

Your example seems to be a bit confusing.

<div id="subcontent">
<?php 
  if ($this->session->flashdata('message')) {
    do something;
    do something else;
?>
    <div class="blah blah">
      <div id="one">some text</div>
      <div id="two">another text</div>
    </div>
<?php
  }
?>
</div>

might imho be a better way! The condition is on the same level as your first div. ist nested in your opening div, so it needs an indent. Same to the following divs, which are nested in the blah-div. An indent of 2 blanks should be enough.

But note, that's just my opinion. The only requirement to good code is that the code must be readable and maintanable. You can find several coding guides in the web (google for coding guidelines php). Look at them, read the explanations and choose these guidelines that you like best!

\$\endgroup\$
  • \$\begingroup\$ I indent w/ 4 spaces (used to coding in Python as well), but I get what you are saying. \$\endgroup\$ – luckytaxi Jul 16 '11 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy