A Guide to Conditional Statements in PHP

February 11th, 2010

When developing a modern web application in PHP, it’s important to allow for logical decisions to be made in the context of your app. For example, user preferences, settings or data may determine a certain element of the site should be shown while on-page objects may also be used to optimize the front end presentation as well. Any time you need to compare a value relative to another value or an anchor to determine what types of content to show, conditional statements provide the underlying logical structure you need.

Related to pure logic, conditionals are if, else statements which allow you to compare results to a certain value. You can nest different logical comparisons within your code, effectively allowing you to compare multiple criterion including values, server data (such as the date) or data from user preferences or cookies.  For large scale sites, having conditional statements can improve the flexibility and load times for your server, ensuring you show the proper page to the right audience.  Optimization of your site to improve conversions involves making sure your code can perform the right user and business tasks properly.

Common Examples of Conditional Statements on PHP Sites

Suppose we’re working with a social networking application and need to categorize users according to their level of activity – we can classify them as beginners, users or power users depending on how many times they have posted to the site in the last 90 days. For the example, let’s suppose beginners post fewer than 9 times, users between 10 and 19 times and power users more than 20 times. We can model this logic as:

<?php
$result = 22;

if ($result >= 20) {
    echo "Power User <br />";
}
elseif ($result >= 10) {
    echo "User <br />";
}
elseif ($result >= 1)
{
    echo "Beginner <br />";
}
else
{
    echo "Welcome New Member <br />";
}
?>

In other instances, the comparative data will directly impact decisions you make at a granular level – allowing you to effectively compare values in multiple dimensions. This data may, for example, affect the price someone pays, their qualification for an application or which redirect they get on a lead form. A common application might be for an e-Commerce store which offers 20% discounts for all purchases above $100 – we would model this login in PHP as:

<?php 
$basketpluss100=true;
$price = 100.00;

if ($basketplus100)
{
    $discount =.20;
    print "You quality for our volume discount so you save $" . $price*$discount;
}
else
{
    print "Add just “100-$price” to your basket and you save save $discount;
} 
?>

In this model, we were able to structure a conditional PHP statement to ensure that customers who meet the checkout requirements would receive a 20% discount. At the same time, you can promote an up-sell offer to users who meet these higher level requirements. For both content and e-Commerce sites, these features are particularly important. As a result, conditional statement can help you meet design, business and site optimization objectives in clean PHP code.

0 responses so far ↓

There are no comments yet...Kick things off by filling out the form below.

Leave a Comment




Webmaster Resources: Web Hosting Blog
© Copyright 2012 - TheWebHostingHero.com
Follow us on TwitterFacebookSubscribe to our RSS Feed