Menu
Coddy logo textTech

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 icon

Challenge

Easy

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

  1. Check if it's NOT raining (use the ! operator)
  2. Check if the person should stay inside: it's raining AND they have neither an umbrella nor a raincoat (use ! with both items)
  3. 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 → false
  • isRaining && !hasUmbrella && !hasRaincoat → true && true && false → false
  • temperature >= 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

?>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals