👨🏼‍💻 Part 5: AND / OR operators

In PHP, we can handle multiple conditions using AND and OR operators inside the if/else statement.
So for example, if we want to show certain content only if two conditions have been met, we would use the AND (&&) operator.
If we want to show certain content if one or other condition has been met, we would use the OR (||) operator.

AND can be written in two ways: “AND”, or “&&”.
OR can be written in two ways: “OR” and “||”.

// If value of variable $name is Alex or Mart, say one thing, otherwise say something else.
if($name == "Alex" || $name == "Mart") {
    echo "Your name is either Alex or Mart.";
}
else
{
     echo "Your name is neither Alex or Mart. Maybe you are Lana, who knows...";
}

Real life example based on ecommerce:
We have an ecommerce page, where there is a search filter. The search result URL looks like this:
https://yoursite.com/?device=phone&brand=apple&status=1

<?php 

// Get the values from the URL and store them as a variable

$device = $_GET['device'];
$brand = $_GET['brand'];
$status = $_GET['status'];

Example #1 – using single AND operator

Let’s say we want to only show a picture of Steve Jobs, when brand is “apple”.
You could use such command.

if($brand == "apple") {
     echo "<img src='https://miro.medium.com/v2/resize:fit:700/0*8m98oBYHgtLUbZCr.jpeg'>";
}

Example #2 – using multiple AND operators

Let’s say we want to show a picture of Steve Jobs holding an iPhone, when

  • brand is “apple” and
  • device is “iphone” and
  • status is “1”.
if($brand == "apple" AND $device == "iphone" AND $status == 1) {
     echo "<img src='https://miro.medium.com/v2/resize:fit:700/0*8m98oBYHgtLUbZCr.jpeg'>";
}

Example #3 – using OR

Let’s say we want to show Apple logo, when either brand is “apple” or device is “iphone”. Makes sense, right?

if($brand == "apple" || $device == "iphone") {
     echo "<img src='https://photos5.appleinsider.com/gallery/47670-93101-000-lead-000-Six-color-Apple-logo-xl.jpg'>";
}

Example #4 – multiple conditions

We want to show Apple logo, only if:

  • brand is “apple” OR
  • device is “iphone”

AND

  • status is 1

So first condition has to be met, and after checking that, also the status needs to be “1”.

if( ($brand == "apple" || $device == "iphone") && status == '1') {
     echo "<img src='https://photos5.appleinsider.com/gallery/47670-93101-000-lead-000-Six-color-Apple-logo-xl.jpg'>";
}

Note that we include the first OR condition inside separate block of parentheses, so it would be taken as a whole condition.

Independent exercise

Instructions:

  1. Create a PHP script that checks if a user can log in based on their username and password.
  2. Define two variables:
    • $username (set it to "admin")
    • $password (set it to "1234")
  3. Use an if-statement with the AND (&&) operator to check if both the username and password match the expected values.
  4. Display "Login successful!" if both are correct; otherwise, display "Invalid username or password.".

Bonus points for using OR (||) operator.

  1. Modify the script to allow an alternative way to log in:
    • If the username is admin and password is 1234 OR
    • username is "guest" AND the password is "0000", display "Guest access granted."

Scroll to Top