Operators have the form of symbols (such as + and -) and combinations of symbols (such as ++ and +=).
Arithmetic operators:
The Arithmetic operators are:
Operator | Description |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus (division remainder) |
++ | Increment |
-- | Decrement |
PHP Code & examples:
<?php
$var1 = 25 + 5 ; // returns 30
$var2 = $var1 / 5 ; // returns 6
$var3 = $var1 + $var2 ; // returns 36
?>
$var1 = 25 + 5 ; // returns 30
$var2 = $var1 / 5 ; // returns 6
$var3 = $var1 + $var2 ; // returns 36
?>
Assignment operators:
The Assignment operators are used to facilitate assigning values to variables:
Operator | Description |
---|---|
+= | Addition and Assignment |
-= | Subtraction and Assignment |
*= | Multiplication and Assignment |
/= | Division and Assignment |
% | Modulo and Assignment |
.= | Concatenation and Assignment |
PHP Code & examples:
<?php
$var = 20 ;
$var = $var + 5 ; // returns 25
// We can use the addition and assignment
$var += 5 ; // returns 25
//these two methods return the same result, but as you see, the second is shorter and better !
?>
$var = 20 ;
$var = $var + 5 ; // returns 25
// We can use the addition and assignment
$var += 5 ; // returns 25
//these two methods return the same result, but as you see, the second is shorter and better !
?>
Comparison Operators:
The Comparison Operators returns a value true or false after comparing two values:
Operator | Description |
---|---|
== | is equal to |
!= | is not equal |
> | is greater than |
< | is less than |
>= | is greater than or equal ro |
<= | is less than or equal to |
the comparaison operators are the most of time used in conditions:
<?php
$var = 20 ;
if ($var1==$var2)
// this instruction will be executed only if $var1 is equal to $var2
if ($var1!=$var2)
// this instruction will be executed only if $var1 is not equal to $var2
if ($var1<$var2)
// this instruction will be executed only if $var1 is less than $var2
// and this is also possible:
$var = ($var1==$var2);
?>
$var = 20 ;
if ($var1==$var2)
// this instruction will be executed only if $var1 is equal to $var2
if ($var1!=$var2)
// this instruction will be executed only if $var1 is not equal to $var2
if ($var1<$var2)
// this instruction will be executed only if $var1 is less than $var2
// and this is also possible:
$var = ($var1==$var2);
?>
Logical Operators:
The logical Operators are used in conditions, they make a relation between two or more condition. Logical operations are also know as Boolean operators:
Operator | Description |
---|---|
&& | And: returns true only if two operands (conditions/expressions) are trues |
|| | Or: returns true if at least one of the two operands returns true |
! | Not: if the operand is true, returns false, else returns true |
xor | Xor: returns true if only one of the operands returns true. If the two operands are true, it returns false. |
Increment and decrement Operators:
The logical Operators are used in conditions, they make a relation between two or more condition. Logical operations are also know as Boolean operators:
Operator | Description |
---|---|
++$var | Increments the variable value before it is used in rest of expression |
--$var | Decrements the variable value before it is used in rest of expression |
$var++ | Increments the variable value after it is used in rest of expression |
$var-- | Decrements the variable value after it is used in rest of expression |