PHP Operators

PHP Operators

The PHP operators can be divided broadly into 5 categories namely,

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Logical Operators

Arithmetic Operators

Operator Description
+ Addition
Subtraction
* Multiplication
/ Division
% Modulus
++ Increment
Decrement

Let’s see a few examples.

$a = 4; $b = 2; $Add= $a + $b; $Sub= $a - $b; $Multip= $a * $b; $Div= $a / $b; $Mod= $a % $b; $Increm= $a++; $Decrem= $b--;

The reult of the above will be:

Add=6 Sub=2 Multip=8 Div=2 Mod=0 Increm=5 Decrem=1

Assignment Operators

Operator Description
= $a = $b; assigns a the value of b (Equal)
+= $a+=$b; means $a = $a+$b; (Addition)
-= $a-=$b; means $a = $a-$b; (Subtraction)
*= $a*=$b; means $a = $a*$b; (Multiplication)
/= $a/=$b; means $a = $a/$b; (Division)
.= $a.=$b; means $a = $a.$b; (Concatenation)
%= $a%=$b; means $a = $a%$b; (Modulus)

Let’s take a look at a few examples:

$a = 4; $b = 2; 1) $a = $b; (assigns a the value 4) 2) $a+=$b; (a will have the value 6) 3) $a-=$b; (a will have the value 2) 4) $a*=$b; (a will have the value 8) 5) $a/=$b; (a will have the value 2) 6) $a%=$b; (a will have the value 0)


Comparison Operators

Operator Description
== $a==$b; will return true if value of a is equal to value of b
!= $a!=$b; will return true if value of a is not equal to value of b
> $a>$b; will return true if value of a is greater than value of b
< $a<$b; will return true if value of a is less than value of b
>= $a>=$b; will return true if value of a is greater than or equal to value of b
<= $a<=$b; will return true if value of a is less than or equal to value of b

Below are a few examples:

$a == $b;// test if two values are equal $a != $b;// test if two values are not equal $a &lt; $b;// test if the first value is less than the second $a &gt; $b;// test if the first value is greater than the second $a &lt;= $b;// test if the first value is less than or equal to the second $a &gt;= $b;// test if the first value is greater than or equal to the second

Logical Operators

Operator Description
&& and; returns true when two or more conditions are be true
|| or; returns true when either of the two conditions is true
! not; negates a given condition
If you have found my website useful, please consider buying me a coffee below 😉