Good coding practice with PHP and HTML

Out of curiosity, I just want to ask about good coding practice. The code below has the same outputs, but has a difference encoding.

So which is better?

This:

<?php foreach ($cart as $item): ?> <tr> <td><?php echo $item['name']; ?></td> <td><?php echo $item['checkin']; ?></td> <td><?php echo $item['checkout']; ?></td> </tr> <?php endforeach; ?> 

Or this one:

 <?php foreach ($cart as $item): echo "<tr>"; echo "<td>$item['name']</td>"; echo "<td>$item['checkin']</td>"; echo "<td>$item['checkout']</td>"; echo "</tr>"; endforeach; ?> 
+4
source share
5 answers

It depends on where and what type of script you are using. Of these two samples, I will choose the first. If you go through the code, you can see the correct indentation with spaces. for debugging code. If you are asking about HTML codes, then work first to check if the code is valid or not. Since code checking makes debugging easier. Here are some of the links, just go through them. We hope you get some to make your code better.

PHP Coding Style - Best Practices

http://net.tutsplus.com/tutorials/php/30-php-best-practices-for-beginners/

http://www.ibm.com/developerworks/opensource/library/os-php-5goodhabits/index.html

http://framework.zend.com/manual/en/coding-standard.coding-style.html

+1
source

Both ways to do this are the correct syntax in PHP. None of them will have an advantage over the other - it all depends on what you prefer. I think the first one looks cleaner and much more picky, but it really is your choice.

+1
source

The first seems better ... consider cases where you need to add some custom rows or columns ...

Also saving individual tags also allows you to customize.

+1
source

I would say that it’s better at first, since when debugging the code or viewing the source of the page, it will support tabs / spaces. It also keeps your page structure separate from your server-side code, so it’s a little cleaner in my opinion.

+1
source

The PHP language takes place as a template language. It still has this aspect, and you must learn to use it.

You can find this article very useful (I do not agree with the use of magic methods). He will explain simple methods for separating business logic from your HTML snippets.

+1
source

All Articles