1

I am attempting to create a program which can calculate the amount of discount for different ages and members. People aged under 12 can a 50% discount and a additional 10% if their a member. People aged under 18 or over 65 can have a 25% discount and an additional 10% on top if their a member. My program only seems to work if the age is below 12 does anyone have any suggestions on how to fix (go easy on me i'm new to programming).

$ticketPrice = 25;
$age = 25;
$membership = 'Yes';
$finalPrice;
$discount;
$memberDis;



if($age < 12) {

    $finalPrice = 25 / 2;

} else if($age < 18) {

$discount = 25 * 0.25;
$finalPrice = 25 - $discount;

} else if($age < 65) {

    $discount = 25 * 0.25;
    $finalPrice = 25 - $discount;
} else if($membership = 'Yes') {

    $discount = $finalPrice * .10;
    $memberDis = $dicount * 100;

}

echo "<br />";
echo "<h1>Ticket Example</h1>";
echo 'Inital Ticket Price: '."&pound".$ticketPrice;
echo "<br />";
echo "Age: ".$age;
echo "<br />";
echo "Member: ".$membership;
echo "<br />";
echo "Final Ticket Price: "."&pound".$finalPrice;
  • Everyone is less than 65. You have no ranges in your conditional tests. You have to test age *and* membership at each level. – Jay Blanchard Oct 16 '18 at 17:38
  • 1
    `$membership = 'Yes'` You're assigning the value instead of comparing. Use `==` instead – aynber Oct 16 '18 at 17:39
  • 1
    You'd also want to remove the else from the membership check, since that would be done for all ages. – aynber Oct 16 '18 at 17:43

2 Answers2

0
  • You should use the variable $ticketPrice instead of hardcoding its value in the if ..else.
  • Inside your conditional statements, just determine the $discount first.
  • Then outside the conditions, calculate the final price
  • Comparison operator is == not =.
  • Membership condition check should be moved out and separate from age checks.

Try

 // Initialize discount to 0
 $discount = 0;
 $finalPrice = $ticketPrice;

 if($age < 12) {

    // if age is less than 12 then 50% discount
    $discount = 50;

 } elseif($age < 18 || $age > 65) {

    // 25% discount for age < 18 or > 65
    $discount = 25;
 }

 if ($membership == 'Yes') {
    // additional 10% discount on membership
    $discount += 10;
 }


// now calculate the final price after removing discount
$finalPrice -= ($finalPrice*$discount/100);
Madhur Bhaiya
  • 28,155
  • 10
  • 49
  • 57
0

You can see a flowchart representation of your code, in order to make easy to understand what are you doing and why it doesn't work. enter image description here

As you can see the membership discount is applied if the age is greater or equals to 65, though you should see the difference between =, == and === operators.

according to your code structure, you should have:

<?php 

$ticketPrice = 25;
$age = 25;
$membership = 'Yes';
$finalPrice;
$discount;
$memberDis;



if($age < 12) {

    $finalPrice = 25 / 2;

} else if($age < 18) {

    $discount = 25 * 0.25;
    $finalPrice = 25 - $discount;

} else if($age < 65) {

    $discount = 25 * 0.25;
    $finalPrice = 25 - $discount;

} 

if($membership === 'Yes') {

    $discount = $finalPrice * .10;
    $finalPrice -= $discount;

}

echo "<br />";
echo "<h1>Ticket Example</h1>";
echo 'Inital Ticket Price: '."&pound".$ticketPrice;
echo "<br />";
echo "Age: ".$age;
echo "<br />";
echo "Member: ".$membership;
echo "<br />";
echo "Final Ticket Price: "."&pound".$finalPrice;
pierDipi
  • 1,388
  • 1
  • 11
  • 20