Recap - Making Decisions
Part of the Fundamentals section of Coddy's PHP journey — lesson 33 of 71.
Challenge
EasyRead 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:
- 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
- If age is less than 13, print
- Membership Discount (use switch): On a new line, print the discount based on membership type:
gold→20% offsilver→10% offbronze→5% off- Any other value →
No discount
- Promo Status (use null coalescing): Create a variable
$appliedPromoset tonull. Use the null coalescing operator to assign$displayPromothe value of$appliedPromoif it's not null, otherwise use thepromoCodeinput. Print$displayPromoon a new line. - Shipping (use ternary): On a new line, use the ternary operator to print
Free shippingifcartTotalis 50 or more, otherwise printStandard shipping.
Example:
If the inputs are 25, gold, SAVE10, and 75, the output should be:
Adult
20% off
SAVE10
Free shippingIf the inputs are 10, basic, WELCOME, and 30, the output should be:
Child
No discount
WELCOME
Standard shippingTry 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
4Comparison & Logical Operators
Comparison OperatorsEquality & IdentityLogical Operators Part 1Logical Operators Part 2Recap - Simple Logic2Variables and Data Types
NumbersStrings and QuotesBooleansNaming ConventionsRecap - Variable InitEmpty VariablesString ConcatenationGetting User InputCast to Different Types5Conditional Logic
If StatementIf - ElseThe Ternary OperatorNull Coalescing OperatorSwitch StatementRecap - Making Decisions3Basic Operators
Arithmetic OperatorsModulo OperatorExponentiation OperatorCombined AssignmentIncrement/DecrementOperator PrecedenceRecap - Simple CalculationsString Operators