Menu
Coddy logo textTech

Recap - Making Decisions

Part of the Fundamentals section of Coddy's PHP journey — lesson 33 of 71.

challenge icon

Challenge

Easy

Read four values from input: an integer age, a string membershipType, a string promoCode, and an integer cartTotal.

Determine the appropriate outputs using the decision-making tools you've learned:

  1. Age Category (use if-elseif-else):
    • If age is less than 13, print Child
    • Otherwise, if age is less than 20, print Teen
    • Otherwise, if age is less than 65, print Adult
    • Otherwise, print Senior
  2. Membership Discount (use switch): On a new line, print the discount based on membership type:
    • gold20% off
    • silver10% off
    • bronze5% off
    • Any other value → No discount
  3. Promo Status (use null coalescing): Create a variable $appliedPromo set to null. Use the null coalescing operator to assign $displayPromo the value of $appliedPromo if it's not null, otherwise use the promoCode input. Print $displayPromo on a new line.
  4. Shipping (use ternary): On a new line, use the ternary operator to print Free shipping if cartTotal is 50 or more, otherwise print Standard shipping.

Example:

If the inputs are 25, gold, SAVE10, and 75, the output should be:

Adult
20% off
SAVE10
Free shipping

If the inputs are 10, basic, WELCOME, and 30, the output should be:

Child
No discount
WELCOME
Standard shipping

Try it yourself

<?php
// Read input
$age = intval(fgets(STDIN));
$membershipType = trim(fgets(STDIN));
$promoCode = trim(fgets(STDIN));
$cartTotal = intval(fgets(STDIN));

// TODO: Write your code below

// 1. Age Category (use if-elseif-else)


// 2. Membership Discount (use switch)


// 3. Promo Status (use null coalescing)
$appliedPromo = null;


// 4. Shipping (use ternary)

?>

All lessons in Fundamentals