1. Home
  2. Computing & Technology
  3. PHP / MySQL

Learn PHP - A Beginner's Guide to PHP Programing

By , About.com Guide

9 of 9

Nested Conditionals

One useful thing to remember about conditional statements is that they can be nested within each other. Below is an example of how the discount program from our example could be written to use nested IF:ELSE statements. There are other ways of doing this - such as using elseif () or switch () but this demonstrates how statements can be nested.

<?php
$age = 30;
$price = 3.00;
if ($age >65)
{
$discount =.90;
print "You have received our senior's discount, your price is $" . $price*$discount;
}
else
{
if ($age <18)
{
$discount =.95;
print "You have received our student's discount, your price is $" . $price*$discount;
}
else
{
print "Sorry you do not qualify for a discount, your price is $" . $price;
}
}
?>

This program will first check if they are eligible for the senior's discount. If they are not, it will then check if they are eligible for a student discount, before returning the non-discounted price.

Explore PHP / MySQL
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. Learn PHP
  5. Learn PHP - PHP Tutorial - Nesting Conditional Statements

©2009 About.com, a part of The New York Times Company.

All rights reserved.