- Back to Home »
- PHP Tutorial »
- PHP Operators
Posted by : senan
Wednesday, February 12, 2014
Welcome! In this section we going to cover differect kinds of operators we use in programming. These operators are common to ever language. So, in PHP they are no diffrent. We will go through each type of operators with examples on how they are done PHP.
Arithmetic Operators
In programming, we often perform arthmenteic operations to calculate values. Below is the list of these oprators with examples.
Operator | Name | Example | Output |
---|---|---|---|
+ | Addition | <?php $num = 5+5; echo $num ?> | 10 |
- | Subtraction | <?php $num = 5-5; echo $num ?> | 0 |
* | Multiplication | <?php $num = 5*5; echo $num ?> | 25 |
/ | Division | <?php $num = 5/5; echo $num ?> | 1 |
Comparison Operators
Comparison Operators prove very useful when checking the status of two variables or values. For example, if you wna tto check if one is equal to the other, we would use = operator. Comparasion operators are used in logic conditional statements (check PHP If...else section) to evalute if the condition is true or false.
Operator | Name | Example | Output |
---|---|---|---|
> | is greater than | 6 > 5 5 > 6 | First example returns true. Second example returns false. |
< | is less than | 6 < 5 5 < 6 | First example returns false. Second example returns true. |
>= | is greater than equal to | 5 >= 4 | returns true |
<= | is less than equal to | 5 <= 4 | returns false |
== | is equal to | 5 = 4 | returns false |
!= | is not equal to | 5 != 4 | returns true |
Logic Operators
Logic operators can be used to combine two or more comparision statements combined into one statement to determine its status. For example, we can check to see if value A greater than value B and value B greater then value C. Let's check out some examples.
Operator | Name | Example | Output |
---|---|---|---|
&& | and | (6 > 5 && 1 < 7) | returns true |
|| | or | (2 > 5 || 1 < 7) | returns true |
If you're not familiar with the logic operations and their usage, please refer to the table below for reference. The table shows what you would get back when performating logic operatioon on two values p and q where T = True and F = False.
p | q | p and q | p or q |
---|---|---|---|
T | T | T | T |
T | F | F | T |
F | T | F | T |
F | F | F | F |