Chapter II - Operators
You may wonder how we may change a value stored in a variable. A possible way to do this is to reassign a new value to this variable, which can be done easily. Another possibility is to use operators, especially Arithmetic operators. PHP has six basic types of operators, namely:
Assignment Operator: =
Arithmetic Operators: +, -, *, /, %
Concatenation Operator: .
Combined Assignment Operators: +=, -=, *=, .=, ++, --
Comparison Operators: ==, >, >=, <, <=, !=
Logic Operators: and, &&, or, ||, not, !
1. Arithmetic Operators:
We already know how the assignment operator "=" works in variable definition, so I wont spend time on it. The Arithmetic operators are used to carry out computations, an example is shown below:
PHP:
$num1 = 3;
$num2 = 2;
$num3 = $num1 + $num2;
$num4 = $num1 - $num2;
$num5 = $num1 * $num2;
$num6 = $num1/$num2;
$num7 = $num1 % $num2;
echo $num1;
echo "</br>";
echo $num2;
echo "</br>";
echo $num3;
echo "</br>";
echo $num4;
echo "</br>";
echo $num5;
echo "</br>";
echo $num6;
echo "</br>";
echo $num7;
PHP:
3
2
5
1
6
1.5
1
2. Concatenation Operator:
The only one Concatenation operator is this cute little thing ., but it is rather useful. It combines two strings into one, a good example is shown below:
PHP:
echo "hello"."world";
PHP:
$num1 = 3;
$num2 = 2;
echo "The first value is ".$num1."</br>The second value is ".$num2."";
echo "</br>The sum is ".($num1+$num2)."</br>The difference is ".($num1 - $num2)."</br>The product is ".($num1*$num2)."";
PHP:
The first value is 3
The second value is 2
The sum is 5
The difference is 1
The product is 6
This type of operators may seem confusing, but they can further simplify your codes and help cut your php file size to minimum. Consider the following PHP codes below:
PHP:
$num = 10;
$num = $num +5;
echo $num;
PHP:
$num = 10;
$num += 5;
echo $num;
PHP:
$num = 5;
$num++
echo $num;
PHP:
$num = 5;
++$num;
echo $num;
PHP:
$num = 3;
$num2 = 2*($num++);
echo $num2;
PHP:
$num = 3;
$num2 = 2*(++$num);
echo $num2;
4. Comparison Operators:
Comparison operators are usually used in switch flows(if...else, switch...case) and loops(while, for loop). They return either true or false, as is illustrated in the example below, assume we have previous defined variable $x = 2.
PHP:
$x < 5; //true
$x == 3; //false
$x >= 10; //false
$x != 4; //true
5. Logic Operators:
Logic operators are quite important in switch flows and loops too in PHP programming. PHP has only four types of logic operators:
PHP:
and, &&: return true only if both or all statements are true
or, ||: return false only if both or all statements are false
xor: return true if one of the statements is true, but not both(applies to two statements only)
!: the 'not' operator
To see how logic operators work, we use the following examples below(assume we've previously defined $x=2):
PHP:
$x <5 or $x>3;
$x >= 1 and $x<=1.5;
$x == 2 xor $x < 7;
6. Operator Precedences:
All of you have learned math in elementary school, so you must be familiar with the idea of precedences. Without using parenthesis, the operators * and / will be executed before + and -. We say that multiplication has higher order precedence than summation. For PHP operators, a list of operator ranked by precedences is shown below:
PHP:
++, --, ()
^
*, /, %
<, <=, >, >=
==, !=
&&
||
=, +=, -=, *=, /=, .=
and
xor
or
PHP:
$num = 2;
$result = (4+5)*$num;
echo $result;
Guess I am done with the tutorial of PHP operators, hope you all have at least understood the basic concept of PHP programming. What you have learned in this chapter will keep recurring in future tutorials, especially comparison and logic operators which are well-integrated in switch flows and loops.
Hall of Famer