I’m a designer & marketer but I’m learning some PHP along the way. A handy little IF statement helped me tighten up how a page looks that post data from a database.
Problem:
This page shows a list of inventory items from a database. There are some fields that might not have content in them – Sale Price for example, not every item will be on sale or have an offer. The page is built in PHP so without some conditional logic this page is going to show the Sale Price content even if it is empty in the database.
Solution:
The handy little IF statement coupled with the !EMPTY condition will get the job done:
<?php if (!empty($item["Inventory"]['sale_price'])) { ?> <div class="inventory_sale_price"> Unit Special: <?php echo $item["Inventory"]['sale_price']; ?> </div> <?php } ?>
The IF is separated from what is displayed since there is div to control the formating. So in summary thanks to the IF and the !EMPTY this page will show what the Special or Sale Price is if there is any content in that field in the database. The ! = NOT in this case so if that field is NOT EMPTY display the content. If it’s empty it will just move on and not display anything.

