PHP AND Operator

Created with Sketch.

PHP AND Operator

Summary: in this tutorial, you’ll learn about the PHP AND operator and how to use it to build a complex logical expression.

Introduction to the PHP AND operator

The logical AND operator accepts two operands and returns true if both operands are true; otherwise, it returns false.

PHP uses the and keyword to represent the logical AND operator:

expression1 and expression2

 

The following table illustrates the result of the and operator:

expression1expression2expression1 and expression2
truetruetrue
truefalsefalse
falsetruefalse
falsefalsefalse

Since PHP keywords are case-insensitive, the AND and and operators are the same:

expression1 AND expression2

 

By convention, you should use the and operator in the lowercase format.

In addition to using the and keyword, PHP uses && as the logical AND operator:

expression1 && expression2

 

The && and and operators return the same result. The only difference between the && and and operators are their precedences.

The and operator has higher precedence than the && operator. The precedence of an operator specifies the order which PHP evaluates.

PHP AND operator examples

Suppose that you want to offer discounts to customers who buy more than three items with a price of more than 99. To determine whether customers can get a discount or not, you can use the AND operator as follows:

<?php

$price = 100;
$qty = 5;

$discounted = $qty > 3 && $price > 99;

var_dump($discounted);

Code language: HTML, XML (xml)

Output:

bool(true)

Code language: JavaScript (javascript)

If you change the $qty to 2, the $discounted will be false like this:

<?php

$price = 100;
$qty = 2;

$discounted = $qty > 3 && $price > 99;

var_dump($discounted);

Code language: HTML, XML (xml)

In practice, you’ll use the logical AND operator in the if, if-else, if-elseif, while, and do-while statements.

Short-circuiting

When the value of the first operand is false, the logical AND operator knows that the result must be also false. In this case, it doesn’t evaluate the second operand. This process is called short-circuiting.

See the following example:

<?php

$debug = false;
$debug && print('PHP and operator demo!');

Code language: HTML, XML (xml)

How it works.

  • First, define the variable $debug and initialize it to false.
  • Second, use the logical AND operator to combine the $debug and print(). Since $debug is false, PHP doesn’t evaluate the call to the print() function.

If you change the $debug to true, you’ll see a message in the output:

<?php

$debug = true;
$debug && print('PHP and operator demo!');

Code language: HTML, XML (xml)

Output:

PHP and operator demo!

Code language: plaintext (plaintext)

Summary

  • Use the PHP AND operator (and, &&) to combine two boolean expressions and returns true if both expressions evaluate to true; otherwise, it returns false.
  • The logical AND operator is short-circuiting.

Leave a Reply

Your email address will not be published. Required fields are marked *