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 ...

asked Jul 16, 2011 at 16:12
\$\endgroup\$

4 Answers 4

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

answered Jul 16, 2011 at 16:19
\$\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..

answered Jul 16, 2011 at 16:17
\$\endgroup\$
3
  • \$\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\$ Commented Jul 16, 2011 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\$ Commented Jul 16, 2011 at 16:34
  • 1
    \$\begingroup\$ i removed the "short" syntax (whatever it's called) due to JS compatibility. \$\endgroup\$ Commented Jul 16, 2011 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>
answered Jul 16, 2011 at 16:21
\$\endgroup\$
1
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!

answered Jul 16, 2011 at 16:40
\$\endgroup\$
1
  • \$\begingroup\$ I indent w/ 4 spaces (used to coding in Python as well), but I get what you are saying. \$\endgroup\$ Commented Jul 16, 2011 at 16:49

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.