How to properly indent PHP/HTML mixed code?

0 votes

When mixing PHP and HTML, what is the proper indentation style to use? Do I indent so that the outputted HTML has correct indentation, or so that the PHP/HTML mix looks properly formatted?

For example, say I have a foreach loop outputting table rows. Which one below is correct?

PHP/HTML mix looks correct:

<table>
  <?php foreach ($rows as $row): ?>
    <tr>
      <?php if ($row->foo()): ?>
        <?php echo $row ?>
      <?php else: ?>
        Something else
      <?php endif ?>
    </tr>
  <?php endforeach ?>
</table>

Outputted HTML looks correct:

<table>
<?php foreach ($rows as $row): ?>
  <tr>
  <?php if ($row->foo()): ?>
    <?php echo $row ?>
  <?php else: ?>
    Something else
  <?php endif ?>
  </tr>
<?php endforeach ?>
</table>

How to solve this?

Oct 29 in PHP by kartik
• 35,300 points
16 views

1 answer to this question.

0 votes

The PHP and the HTML should each be indented so that they are correct with respect to themselves, in source form, irrespective of each other and of outputted form:

<table>
<?php foreach ($rows as $row): ?>
    <tr>
    <?php if ($row->foo()): ?>
        <?php echo $row ?>
    <?php else: ?>
        Something else
    <?php endif ?>
    </tr>
<?php endforeach ?>
</table>

Hope it works!!

answered Oct 29 by Niroj
• 76,540 points

Related Questions In PHP

0 votes
1 answer

How to prevent XSS with HTML/PHP?

Hello, HTML Encoding htmlspecialchars will convert any "HTML special characters" ...READ MORE

answered Apr 1 in PHP by Niroj
• 76,540 points
187 views
0 votes
0 answers
0 votes
1 answer

How to convert PHP code to MySQL query to CSV?

Hello @kartik, Try this: SELECT * INTO OUTFILE "c:/mydata.csv" FIELDS ...READ MORE

answered Aug 20 in PHP by Niroj
• 76,540 points
59 views
0 votes
1 answer

How to execute PHP code from the command line?

Hello @kartik, the php command offers two switches to ...READ MORE

answered Oct 1 in PHP by Niroj
• 76,540 points
50 views
0 votes
1 answer

jQuery AJAX fires error callback on window unload - how do I filter out unload and only catch real errors?

Hello, In the error callback or $.ajax you have three ...READ MORE

answered Apr 27 in Java-Script by Niroj
• 76,540 points
437 views
0 votes
1 answer

How do I pass command line arguments to a Node.js program?

Hello @kartik, If your script is called myScript.js ...READ MORE

answered May 5 in Java-Script by Niroj
• 76,540 points
670 views
0 votes
1 answer

Error:Issue when trying to use IN() in wordpress database

Hello @kartik, Try this code : // Create an ...READ MORE

answered May 8 in PHP by Niroj
• 76,540 points
88 views
+2 votes
1 answer

How do I debug Node.js applications?

Hello @kartik, Use node-inspector  from any browser supporting WebSocket. Breakpoints, ...READ MORE

answered Jul 8 in Node-js by Niroj
• 76,540 points
87 views
0 votes
1 answer

How to properly indent PHP/HTML mixed code?

Hello @kartik, The PHP and the HTML should ...READ MORE

answered Oct 30 in PHP by Niroj
• 76,540 points
22 views
0 votes
1 answer

How do I add PHP code/file to HTML(.html) files?

Hello @kartik, You can't run PHP in .html ...READ MORE

answered Oct 22 in PHP by Niroj
• 76,540 points
22 views