Posted by tutor | Posted in PHP Tutorial | Posted on 03-08-2010-05-2008
0
We often need to increment a variable by a fixed amount. For eg a variable which is being used to count something, may be a counter for a loop or some other purpose, we need to constantly increment it by 1.
However, there is another way for doing this.
This combined assignment/arithmetic operator would accomplish the same task as the above code.
Although this combined operator shortens the code but sometimes for those who are not accustomed to such programming constructs, this may create confusion and may make the code unreadable.0
| Operator |
Description |
Example |
Expanded Form |
| += |
Plus Equals |
$x += 2; |
$x = $x + 2; |
| -= |
Minus Equals |
$x -= 4; |
$x = $x – 4; |
| *= |
Multiply Equals |
$x *= 3; |
$x = $x * 3; |
| /= |
Divide Equals |
$x /= 2; |
$x = $x / 2; |
| %= |
Modulo Equals |
$x %= 5; |
$x = $x % 5; |
| .= |
Concatenate Equals |
$my_str.=”PHP”; |
$my_str = $my_str .”PHP”; |
Posted by tutor | Posted in PHP Tutorial | Posted on 03-08-2010-05-2008
0
Operators are used to manipulate the values or directly or to operate on variables to manipulate values.
All the operators are categorized under the following:
- Assignment Operators
- Arithmetic Operators
- Comparison Operators
- String Operators
This section lists the different operators available in PHP.
Assignment Operators
Assignment operators assign a value to a variable, or the value of a variable to another variable.
$my_var = 4;
$another_var = $my_var;
Now both $my_var and $another_var contain the value 4.
Assignments can also be used in conjunction with arithmetic operators, which we will be studying later in this tutorial.
Arithmetic Operators
| Operator |
Description |
Example |
| + |
Addition |
2 + 4 |
| - |
Subtraction |
6-2 |
| * |
Multiplication |
5*3 |
| / |
Division |
15/3 |
| % |
Modulus |
43%10 |
$addition= 2+3;
$subtraction= 3-2;
$multiplication= 2*3;
$division= 22/11;
$modulus = 5 % 2;
Comparison Operators
Comparison Operators are used to compare the values of two operators. These operators are used inside conditional statements and evaluate to either true or false. PHP provides all the comparison operators that are provided by other programming language.
Assume: $x = 4 and $y = 5;
| Operator |
Description |
Example |
Result |
| == |
Equal To |
$x == $y |
false |
| != |
Not Equal To |
$x != $y |
true |
| < |
Less Than |
$x < $y |
true |
| > |
Greater Than |
$x > $y |
false |
| <= |
Less Than or Equal To |
$x <= $y |
true |
| >= |
Greater Than or Equal To |
$x >= $y |
false |
String Operators
As we have learned before the dot operator is used to concatenate or join two strings.
$a_string=”Hello”;
$b_string=” Tutor”;
$new_string=$a_string . $_string;
Echo $new_string;