# How To Use Logical Operators In Php

WHAT IS A LOGICAL OPERATOR?

logical operator is a symbol or word used to connect two or more expressions such that the value of the compound expression produced depends only on that of the original expressions and on the meaning of the operator.[1] Common logical operators include AND, OR, and NOT.

logical operation is a special symbol or word that connects two or more phrases of information. It is most often used to test whether a certain relationship between the phrases is true or false.

What are the different types of logical operators?

These logical operators are used to compare two values of the same type.

• true. to the first expression and . false. to the second;
• false. to the first expression and . true. to the second; and,
• false. to both statements.

## Fundamental logic operations

The following seven logic operations take inputs that are either true (1) or false (0) and produce a single output value that is also true or false.

Most of these operations can take more than two inputs, except for the NOT operation which takes only one input. Below are examples using only one or two inputs, which is what usually happens inside a computer.

The operations are listed below. Click a link for an operation to learn more.

Which are the logical operators in PHP?

How are logical operators used?

Logical operators are used in logical and arithmetic comparisons, and they return TRUE (i.e., logical 1) if the expression evaluates to nonzero or FALSE (i.e., logical 0) if the expression evaluates to zero.

HOW TO USE LOGICAL OPERATORS IN PHP

## What is Operators in PHP

Operators are symbols that tell the PHP processor to perform certain actions. For example, the addition (`+`) symbol is an operator that tells PHP to add two variables or values, while the greater-than (`>`) symbol is an operator that tells PHP to compare two values.

The following lists describe the different operators used in PHP.

## PHP Arithmetic Operators

The arithmetic operators are used to perform common arithmetical operations, such as addition, subtraction, multiplication etc. Here’s a complete list of PHP’s arithmetic operators:

The following example will show you these arithmetic operators in action:

#### Example

``````<?php
\$x = 10;
\$y = 4;
echo(\$x + \$y); // 0utputs: 14
echo(\$x - \$y); // 0utputs: 6
echo(\$x * \$y); // 0utputs: 40
echo(\$x / \$y); // 0utputs: 2.5
echo(\$x % \$y); // 0utputs: 2
?>``````

## PHP Assignment Operators

The assignment operators are used to assign values to variables.

The following example will show you these assignment operators in action:

#### Example

``````<?php
\$x = 10;
echo \$x; // Outputs: 10

\$x = 20;
\$x += 30;
echo \$x; // Outputs: 50

\$x = 50;
\$x -= 20;
echo \$x; // Outputs: 30

\$x = 5;
\$x *= 25;
echo \$x; // Outputs: 125

\$x = 50;
\$x /= 10;
echo \$x; // Outputs: 5

\$x = 100;
\$x %= 15;
echo \$x; // Outputs: 10
?>``````

## PHP Comparison Operators

The comparison operators are used to compare two values in a Boolean fashion.

The following example will show you these comparison operators in action:

#### Example

``````<?php
\$x = 25;
\$y = 35;
\$z = "25";
var_dump(\$x == \$z);  // Outputs: boolean true
var_dump(\$x === \$z); // Outputs: boolean false
var_dump(\$x != \$y);  // Outputs: boolean true
var_dump(\$x !== \$z); // Outputs: boolean true
var_dump(\$x < \$y);   // Outputs: boolean true
var_dump(\$x > \$y);   // Outputs: boolean false
var_dump(\$x <= \$y);  // Outputs: boolean true
var_dump(\$x >= \$y);  // Outputs: boolean false
?>``````

## PHP Incrementing and Decrementing Operators

The increment/decrement operators are used to increment/decrement a variable’s value.

The following example will show you these increment and decrement operators in action:

#### Example

``````<?php
\$x = 10;
echo ++\$x; // Outputs: 11
echo \$x;   // Outputs: 11

\$x = 10;
echo \$x++; // Outputs: 10
echo \$x;   // Outputs: 11

\$x = 10;
echo --\$x; // Outputs: 9
echo \$x;   // Outputs: 9

\$x = 10;
echo \$x--; // Outputs: 10
echo \$x;   // Outputs: 9
?>``````

## PHP Logical Operators

The logical operators are typically used to combine conditional statements.

The following example will show you these logical operators in action:

#### Example

``````<?php
\$year = 2014;
// Leap years are divisible by 400 or by 4 but not 100
if((\$year % 400 == 0) || ((\$year % 100 != 0) && (\$year % 4 == 0))){
echo "\$year is a leap year.";
} else{
echo "\$year is not a leap year.";
}
?>``````

## PHP String Operators

There are two operators which are specifically designed for strings

The following example will show you these string operators in action:

#### Example

``````<?php
\$x = "Hello";
\$y = " World!";
echo \$x . \$y; // Outputs: Hello World!

\$x .= \$y;
echo \$x; // Outputs: Hello World!
?>``````

## PHP Array Operators

The array operators are used to compare arrays:

The following example will show you these array operators in action:

#### Example

``````<?php
\$x = array("a" => "Red", "b" => "Green", "c" => "Blue");
\$y = array("u" => "Yellow", "v" => "Orange", "w" => "Pink");
\$z = \$x + \$y; // Union of \$x and \$y
var_dump(\$z);
var_dump(\$x == \$y);   // Outputs: boolean false
var_dump(\$x === \$y);  // Outputs: boolean false
var_dump(\$x != \$y);   // Outputs: boolean true
var_dump(\$x <> \$y);   // Outputs: boolean true
var_dump(\$x !== \$y);  // Outputs: boolean true
?>``````

## PHP Spaceship Operator PHP 7

PHP 7 introduces a new spaceship operator (`<=>`) which can be used for comparing two expressions. It is also known as combined comparison operator.

The spaceship operator returns `0` if both operands are equal, `1` if the left is greater, and `-1` if the right is greater. It basically provides three-way comparison as shown in the following table:

The following example will show you how spaceship operator actually works:

#### Example

Run this code »

``````<?php
// Comparing Integers
echo 1 <=> 1; // Outputs: 0
echo 1 <=> 2; // Outputs: -1
echo 2 <=> 1; // Outputs: 1

// Comparing Floats
echo 1.5 <=> 1.5; // Outputs: 0
echo 1.5 <=> 2.5; // Outputs: -1
echo 2.5 <=> 1.5; // Outputs: 1

// Comparing Strings
echo "x" <=> "x"; // Outputs: 0
echo "x" <=> "y"; // Outputs: -1
echo "y" <=> "x"; // Outputs: 1
?>``````

Why are logical operators important?

Logical operators are very important in any programming language and they help us take decisions based on certain conditions. Suppose we want to combine the result of two conditions, then logical AND and OR logical operators help us in producing the final result. Called Logical AND operator.

What are the advantages of using Boolean operators?

• Boolean Operators: And, Or, Not.
• Allows us to broaden our search by adding like terms (using “or”)
• Allows us to narrow our search by searching more than one topic at a time (using “and”)
• Allows us to eliminate unwanted results (using “not”)