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

Learn PHP - A Beginner's Guide to PHP Programing

By Angela Bradley, About.com

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.

Index: Learn PHP - A Beginner's Guide to PHP Programing

  1. Basic PHP Syntax
  2. Comments
  3. PRINT and ECHO Statements
  4. Variables
  5. Arrays
  6. Operands
  7. Operators
  8. Conditional Statements
  9. Nested Conditionals

9 of 9

Explore PHP / MySQL

More from About.com

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

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

All rights reserved.