PDA

View Full Version : Hall of Famer's PHP Tutorials: Chapter 2 - PHP Operators


Hall of Famer
02-22-2011, 07:26 PM
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:


$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;
The results are:


3
2
5
1
6
1.5
1
Now you can see how operators work. Unfortunately, this is a quite amateur way of programming, which can be significantly improved with the usage of Concatenation operator.


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:


echo "hello"."world";
The result is a single string 'helloworld' on the screen. The magic of Concatenation operator is much much more than that, we can not only play around with combination of strings, but also variables. We can easily rewrite the codes in part a as the form below:


$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)."";
You will find the following results in your screen, which significantly reduces the number of lines we write in a php file.

The first value is 3
The second value is 2
The sum is 5
The difference is 1
The product is 6

3. Combined Assignment Operators:
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:


$num = 10;
$num = $num +5;
echo $num;
This works perfectly, but is not a professional way of programming. With slight modification using combined assignment operator +=, we can give our script file a much better looking:


$num = 10;
$num += 5;
echo $num;
Both php codes yield the result 15 on the screen, combined assignment operators do make our codes look a lot neater. For increments of 1, the following trick can be played to further simplify our scripts:


$num = 5;
$num++
echo $num;
or:


$num = 5;
++$num;
echo $num;
Both techniques will give result 6 on the screen. One may wonder what the difference is with $var++ or ++$var. Lemme use the next examples to illustrate this point:


$num = 3;
$num2 = 2*($num++);
echo $num2;

$num = 3;
$num2 = 2*(++$num);
echo $num2;
Interestingly, the first result is 6 while the second is 8. We can conclude that the operator causes variable $num to increment after multiplication takes place, while the second operator increments before multiplication occurs.


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.


$x < 5; //true
$x == 3; //false
$x >= 10; //false
$x != 4; //true
A very common programming error is to misuse comparison operator == as assignment operator =. It usually will not generate any error message, which makes it even harder to detect.


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:


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
The only difference between and/&&, or/|| is when it comes to operator precedence. and/or are at lower precedence than arithmetic operators +, -, *, /, while &&/|| execute before arithmetic operators if parenthesis() is not used. It may sound confusing at this moment, I will demonstrate this point in more details in future tutorials.

To see how logic operators work, we use the following examples below(assume we've previously defined $x=2):


$x <5 or $x>3;
$x >= 1 and $x<=1.5;
$x == 2 xor $x < 7;
The first line returns true since or operator is true so long as one of the statement. The second line returns false since and operator is true only if both statements return true. The third line seems a little bit tricky though. Both statements($x == 2, $x < 7) are true, but the entire line returns false due to the nature of xor operator.


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:


++, --, ()
^
*, /, %
<, <=, >, >=
==, !=
&&
||
=, +=, -=, *=, /=, .=
and
xor
or
Looks complicated, isnt it? I wouldnt bother myself with memorizing this table or operator precedences. Instead, I'd rather simply enclose statements in parenthesis() to avoid confusion. Good examples are:


$num = 2;
$result = (4+5)*$num;
echo $result;
This gives you a result of 18, as we have expected.


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