PHP Operators


Share

PHP offers a lot of powerful operators, ranging from arithmetic,
string, and logical operators to operators for assignment, comparison, and more.
various types operators in PHP various types operators in PHP.


1) Arithmetic Operators

Used for Basic mathematics operations.

<?php
$a=3;
$b=9;
$c=$a+$b;
echo $c;
?>

2) Comparison Operators

Used to Comparing two values, also know as Relational operator.

<?php
  $a = 2; $b = 3;
  if ($a > $b)  echo "$a is greater than $b";
  if ($a < $b)  echo "$a is less than $b";
  if ($a == $b) echo "$a is equal to $b";
?>
    OUTPUT:
     2 is less than 3 

3) Logical operators

Logical operators produce true-or-false results, and therefore are also known as Boolean operators.
for example, AND, OR, XOR, NOT.

<?php
$a = 1; $b = 0; 
echo ($a AND $b) ;
echo ($a or $b);
echo ($a XOR $b);
echo !$a;
?>
    OUTPUT:
     NULL 1 1 NULL

4) Conditional Operators

Used to check the condition of statement also know as ternary operator ?,:

<?php
 $enough = $fuel <= 1 ? FALSE : TRUE;
?>

5) Assignment Operators

=
+=
-=
*=
/=
.=
%=
&=
!=
^=
<<=
>>=
are assignment operators.

<?php
 $a=4;
 $b=5;
 $b+=$a;
 echo $b;
?>
    OUTPUT:
    9