PHP Operator
Same as many Programming Languages php have its operators
In this Article lets go through 1 by 1.
PHP Arithmetic Operators
Refer the below for the types of Arithmetic operators in PHP.
Operator | Example | Results |
---|---|---|
Negation
(-$a) |
<?php $a = 5; echo -$a ; ?> |
-5 |
Addition
(+) |
<?php $a = 5; $b = 6; echo $b+$a ; ?> |
11 |
Subtraction
(-) |
<?php $a = 5; $b = 6; echo $b-$a ; ?> |
1 |
Mutiplication
(*) |
<?php $a = 5; $b = 6; echo $b*$a ; ?> |
30 |
Division
(/) |
<?php $a = 5; $b = 6; echo $b/$a ; ?> |
1.2 |
Modulus
(%) |
<?php $a = 6; $b = 2; $c =($a%$b)?'has a Remainder':'no remainder'; echo $c; ?> // 6 Modulus 2 there are no remainder //If no remainder = true output no remainder |
no Remainder |
Exponential
(**) |
<?php $a = 6; $b = 2; $c = $a ** $b; echo $c; ?> //6 power of 2 is equal to 36 |
36 |
PHP Comparisons Operators
Refer the below for the types Comparisons operators in PHP.
Operator | Example | Results |
---|---|---|
Equal
(==) |
<?php $a = 5; $b=5; $c=($a==$b)?'True':'False' ; echo $c; ?> //$a is Equal to $b so the result is True |
True |
Identical
(===) |
<?php $a = 'String'; $b=5; $c=($a===$b)?'True':'False' ; echo $c; ?> //$a is a String $b is a number they are not identical |
False |
Not Equal
(!=)/(<>) |
<?php $a =6; $b=5; $c=($a!=$b)?'True':'False' ; echo $c;?> //$a is 6 and $b is 5 so they are not equal |
True |
Less Than (<) |
<?php $a =6; $b=5; $c=($a<$b)?'True':'False' echo $c;; ?> //$a is 6 and $b is 5 so a is less b , is false |
False |
Greater Than (>) |
<?php $a =6; $b=5; $c=($a>$b)?'True':'False' echo $c;; ?> //$a is 6 and $b is 5 so a is Greater than b is |
True |
Less Than or equal to (<=) |
<?php $a =4; $b=5; $c=($a<=$b)?'True':'False' echo $c;; ?> //$a is 4 and $b is 5 so a is less than equal to b , |
True |
More Than or equal to (<=) |
<?php $a =4; $b=5; $c=($a>=$b)?'True':'False' echo $c;; ?> //$a is 4 and $b is 5 so a is More than equal to b , |
False |
SpaceShip (<=>) |
<?php $a =4; $b=5; $c=($a<=>$b)?'True':'False' echo $c;; ?> //$a is less than or equal to |
True |
Null Coalese($a??$b?) |
<?php $a = 4; $b=5; $c=$a??$b?'True':'False' ; echo $c; ?> //the First Column |
True |
PHP Logical Operators
Refer the below for the types of Logical operators in PHP.
Operator | Example | Results |
---|---|---|
and (&&) |
<?php $a = True;$b= True; $c=($a and $b)?'True':'False' ; echo $c; |
True |
or (||) |
<?php $a = False;$b= True; $c=($a or $b)?'True':'False' ; echo $c; |
True |
xor |
<?php $a = False;$b= True; $c=($a xor $b)?'True':'False' ; echo $c; //Either a or b is true but not |
False |
Check out echo vs Print