PHP Operators Part 2 - Comparison Operator, Logical Operator, Conditional Operator

PHP Operators Part 2 - Comparison Operator, Logical Operator, Conditional Operator

Today, we’ll learn learn some of the operators which was not discussed in previous lecture. This will be helpful for our next if-else-elseif lecture.

  1. Comparison Operator
  2. Logical Operator
  3. Conditional Operator

Comparison Operator

Comparison Operator are used to compare two values. Let’s get the list of Comparison operators -

Operator Name Example Result
== Equal $a == $b Return true if $a and $b's value are equal.
=== Identical (Triple Equal) $a === $b Return true if $a and $b are identical with case sensativity
!= Not Equal $a != $b Return true if $a and $b's value are not equal.
!== Not Identical (Not Triple Equal) $a !== $b Return true if $a and $b are not identical with case sensativity
<> Not Equal $a <> $b Return true if $a and $b's value are not equal.
< Less than $a < $b Return true if $a is less than $b
> Greater than $a > $b Return true if $a is greater than $b
<= Less than or Equal $a <= $b Return true if $a is less than or equal to $b
>= Greater than or Equal $a >= $b Return true if $a is greater than or equal to $b
<=> Spaceship or Three way comparison operator $a <=> $b Three values => -1, 0, 1. Return 0 if both equal, 1 if Left is greater, -1 if right is greater.

Examples of Comparison Operator

<?php
$number1 = 10;
$number2 = 20;
var_dump($number1 > $number2); // bool(false)

$number1 = 10;
$number2 = 10;
var_dump($number1 >= $number2); // bool(true)

$number1 = 10;
$number2 = 5;
var_dump($number1 < $number2); // bool(false)

$number1 = 10;
$number2 = 5;
var_dump($number1 <=> $number2); // int(1)

$number1 = 10;
$number2 = 10;
var_dump($number1 <=> $number2); // int(0)

$number1 = 10;
$number2 = 50;
var_dump($number1 <=> $number2); // int(-1)

?>

Logical Operator

It’s some logical operations operator or Circuits operators. This only works on boolean (1, 0) values and return type will also be boolean. Let’s get the list of Logical operators -

Operator Name Example Result
and And Operator $a and $b Return true if $a and $b both are true
or Or Operator $a or $b Return true if $a or $b any of them are true
xor Or Operator $a xor $b Return true either $a or $b is true, but not both.
! Not Operator !$a Return true if $a is false.
&& And Operator $a && $b Return true if $a and $b both are true
|| Or Operator $a && $b Return true if $a or $b any of them are true

Example of Logical Operator

<?php
$a = true;
$b = false;

var_dump($a and $b); // bool(false)

var_dump($a or $b); // bool(true)

var_dump($a xor $b); // bool(true)

var_dump($a and $b); // bool(false)

var_dump(!$b); // bool(true)

var_dump($a && $b); // bool(false)

var_dump($a || $b); // bool(true)
?>

Conditional Operator

Conditional Operator is for checking some condition -

Operator Name Example Result
?: Ternary Operator $a = condition ? result1 : result2 Return condition is true, return $result1 else return $result2
?? [Supported from PHP7.x] Null coalescing Operator $a = result1 ?? result2 Return $result1 if it is exist and not null, otherwise return $result2

Example of Conditional Operator

<?php
$a = true;
$result = $a ? "A is true" : "A is false";

echo $result . '<br>';  // A is true

$val = null;
echo $val ?? "Value is not set"; // Value is not set
echo '<br>';

$val = "Hi, Nice val.";
echo $val ?? "Value is not set"; // Hi, Nice val.

?>

Previous
PHP Operators - Arithmetic operator, Assignment Operator, Increment-decrement Operator
Next
PHP If-else-elseif and Switch-case