1. Computing

Learn PHP - A Beginner's Guide to PHP Programing

By

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.

Related Video
Basic PHP Syntax
Using PHP With HTML
  1. About.com
  2. Computing
  3. PHP / MySQL
  4. Learn PHP
  5. Nesting Conditional Statements in PHP

©2014 About.com. All rights reserved.