PHP Operators

An operator is a symbol that manipulates one or more values, usually producing a new value in the process. Meanwhile, an expression in PHP is anything that evaluates to value; this can be any combination of values, variables, operators, and functions. In the preceding example, $x + $y is an expression. Here are some more examples of expressions:
$x + $y + $z
$x -$y
$x 5
true
gettype( $test_var )

The values and variables that are used with an operator are known as operands.
Operator Types

  • Arithmetic Operators
  • Comparison Operators
  • Logical Operators
  • Assignment Operators
  • Conditional Operators(?:)
  • Operator Precedence

Arithmetic Operators

All mathematical operations are performed using operators that fall under the category of arithmetic operators. A list of supported operators have been given below .

Addition(+)

This operators perform addition between two numbers. If a and b are two variables holding 7 and 4 respectively.
Then

<?php
a=7;
b=4;
$c=$a+$b;
echo $c;

Output:
11

Multiplication(*)

This operator performs multiplication between two numbers. If a and b are two variable holding 7 and 4 respectively.

<?php
$a=7;
$b=4;
$c=$a*$b;
echo $c;

Output:
28

Subtraction(-)

This operators perform subtraction between two numbers. If a and b are two variable holding 7 and 4 respectively.

<?php
$a=7;
$b=4
$c=$b-$c;
echo $c;

Output:
3

Division(/)

This operator performs division between two numbers. If a and b are two variable holding 6 and 3 respectively.

<?php
$a=6;
$b=3;
$c=$a/$b;
echo $c;

Output:
2

Modules (%)

This operator returns the reminder left after dividing the two numbers.

<?php
$a=10;
$b=5;
$c=$a%$b;
echo $c;
// Output will be 0;

$x=10;
$y=7;
$z=$x%$b;
echo $z;
// Output will be 3

Exponentiation(**)

This operator is used to calculate a base raised to the power of the exponent. It takes two parameters which are the base and exponent and returns the desired answer. If a and b are two variable holdings 5 and 3 respectively.Then

<?php
$a = 10;  
$b = 3;
echo $a ** $b;

Output:
125

Comparison Operators

All operations involving comparison are performed using comparison operators. A list of the operators supported by this programming language is given below-

NOT EQUAL TO(!=,<>)

This operator is applied on two operands. It checks equality between two operands. If they are equal, it returns FALSE else it return TRUE.

<?php
$a=10;
$b=10;
var_dump($a!=$b);

Output:
bool(false)

EQUAL TO(==)

This operator is applied on two operands. It checks equality between two operands. If they are equal, it return TRUE else it return FALSE.

<?php
$a=10;
$b=10;
var_dump($a==$b);

Output:
bool(true)

GREATER THAN(>)

This operator is applied on twp operands. It checks equality between two operands. If the first operand is greater than the second operand, it returns TRUE else it return FALSE.

<?php
$a=10;
$b=5;
var_dump($a > $b);

Output:
bool(true)

LESS THAN (<)

This operator is applies on two operands. It checks equality between two operands. If the first operand is less than the second operand , it returns TRUE else it return FALSE.

<?php
$a=10;
$b=5;
var_dump($a < $b);

Output:
bool(true)

GREATER THAN OR EQUAL TO(>=)

This is applied on two operands. It checks equality between two operands. If the first operands is greater than or equal to the second operand, it return TRUE else it return FALSE.

<?php
$a=10;
$b=5;
var_dump($a >= $b);

Output:
bool(true)

LESS THAN OR EQUAL TO(>=)

This is applied on two operands. It checks equality between two operands. If the first operand is less than or equal to the second operand, it returns TRUE else it returns FALSE.

<?php
$a=10;
$b=5;
var_dump($a <= $b);

Output:
bool(true)

IDENTICAL(===)

This is applied on two operands. It checks equality between two operands. If the first operand is equal to the second operand and they are of the same type, it returns TRUE else it returns FALSE.

<?php
$a=10;
$b="10";
var_dump($a === $b);

Output:
bool(false)

NOT IDENTICAL(!==)

This is applied on two operands. It checks equality between two operands. If the first operand is not equal to the second operand and they are not of the same type, it returns TRUE else it returns FALSE.

<?php
$a=10;
$b="10";
var_dump($a === $b);

Output:
bool(tue)

SPACESHIP(<=>)

Return an integer less than, equal to, or greater than zero, depending on if $a is less than, equal to, or greater than $y.

<?php
$a=10;
$b=15;
echo ($a<=>$b); //return -1 because $a less than $y

$a=10;
$b=10;
echo ($a<=>$b); //return 0 because values are equal.

$a=15;
$b=10;
echo ($a<=>$b); //return +1 because $a is greater than $y.
Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy