Logical Operators Part 2
Part of the Fundamentals section of Coddy's PHP journey — lesson 26 of 71.
The NOT operator (!) flips a boolean value to its opposite. If something is true, it becomes false, and vice versa:
<?php
$isLoggedIn = false;
$showLoginButton = !$isLoggedIn;
var_dump($showLoginButton); // bool(true)
$isLoggedIn = true;
$showLoginButton = !$isLoggedIn;
var_dump($showLoginButton); // bool(false)
?>You can combine multiple logical operators in a single expression. When you do, use parentheses to make your intentions clear:
<?php
$age = 25;
$hasTicket = true;
$isVIP = false;
// Can enter if: has ticket AND (is adult OR is VIP)
$canEnter = $hasTicket && ($age >= 18 || $isVIP);
var_dump($canEnter); // bool(true)
?>Without parentheses, && is evaluated before ||. However, using parentheses makes your code easier to read and prevents mistakes. When combining operators, always group conditions logically to express exactly what you mean.
Challenge
EasyRead four values from input: an integer temperature, a string isRaining (either "true" or "false"), a string hasUmbrella (either "true" or "false"), and a string hasRaincoat (either "true" or "false").
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 it's NOT raining (use the
!operator) - Check if the person should stay inside: it's raining AND they have neither an umbrella nor a raincoat (use
!with both items) - Check if the person can go outside comfortably: the temperature is at least 15 AND (it's not raining OR they have an umbrella or raincoat)
Example:
If the inputs are 20, true, false, and true, the output should be:
bool(false)
bool(false)
bool(true)Explanation:
!isRaining→ !true → falseisRaining && !hasUmbrella && !hasRaincoat→ true && true && false → falsetemperature >= 15 && (!isRaining || hasUmbrella || hasRaincoat)→ true && (false || false || true) → true && true → true
Cheat sheet
The NOT operator (!) flips a boolean value to its opposite:
<?php
$isLoggedIn = false;
$showLoginButton = !$isLoggedIn;
var_dump($showLoginButton); // bool(true)
$isLoggedIn = true;
$showLoginButton = !$isLoggedIn;
var_dump($showLoginButton); // bool(false)
?>You can combine multiple logical operators in a single expression. Use parentheses to make your intentions clear:
<?php
$age = 25;
$hasTicket = true;
$isVIP = false;
// Can enter if: has ticket AND (is adult OR is VIP)
$canEnter = $hasTicket && ($age >= 18 || $isVIP);
var_dump($canEnter); // bool(true)
?>Without parentheses, && is evaluated before ||. However, using parentheses makes your code easier to read and prevents mistakes.
Try it yourself
<?php
// Read input values
$temperature = intval(fgets(STDIN));
$isRainingStr = trim(fgets(STDIN));
$hasUmbrellaStr = trim(fgets(STDIN));
$hasRaincoatStr = trim(fgets(STDIN));
// TODO: Convert string values to booleans by comparing them to "true"
// TODO: Evaluate and print the three conditions using var_dump():
// 1. Check if it's NOT raining
// 2. Check if the person should stay inside (raining AND no umbrella AND no raincoat)
// 3. Check if the person can go outside comfortably
?>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
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