Menu
Coddy logo textTech

Recap - Simple Logic

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

challenge icon

Challenge

Easy

Read 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:

  1. Check if the person qualifies for a youth discount: age is less than 25 AND is a student
  2. 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)
  3. 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 → true
  • balance >= 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