Recap - Simple Logic
Part of the Fundamentals section of Coddy's PHP journey — lesson 27 of 71.
Challenge
EasyRead five values from input: an integer age, an integer balance, a string isStudent (either "true" or "false"), a string hasPromoCode (either "true" or "false"), and an integer itemPrice.
Convert the string values to booleans by comparing them to "true".
Evaluate the following three conditions and print the result of each using var_dump(), each on a new line:
- Check if the person qualifies for a youth discount: age is less than 25 AND is a student
- Check if the person can afford the item with a discount: balance is greater than or equal to itemPrice OR (balance is at least half of itemPrice AND hasPromoCode is true)
- Check if the person gets priority checkout: (age is at least 65 OR isStudent is true) AND balance is greater than 0 AND NOT hasPromoCode
Example:
If the inputs are 22, 50, true, false, and 80, the output should be:
bool(true)
bool(false)
bool(true)Explanation:
age < 25 && isStudent→ 22 < 25 && true → truebalance >= itemPrice || (balance >= itemPrice / 2 && hasPromoCode)→ 50 >= 80 || (50 >= 40 && false) → false || false → false(age >= 65 || isStudent) && balance > 0 && !hasPromoCode→ (false || true) && true && true → true
Try it yourself
<?php
// Read input values
$age = intval(fgets(STDIN));
$balance = intval(fgets(STDIN));
$isStudentStr = trim(fgets(STDIN));
$hasPromoCodeStr = trim(fgets(STDIN));
$itemPrice = intval(fgets(STDIN));
// Convert string values to booleans
$isStudent = $isStudentStr === "true";
$hasPromoCode = $hasPromoCodeStr === "true";
// TODO: Write your code below
// 1. Check if the person qualifies for a youth discount
// 2. Check if the person can afford the item with a discount
// 3. Check if the person gets priority checkout
?>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