PHP Operators This section lists the different operators used in PHP with some code example:Operator is a symbol or special character which is used in an expression for manipulation assess or variable and give a result. Arithmetic operators: Operator
| Description
| Example
| Result
| | + | Addition | x=2 x+2 | 4 | | - | Substraction | x=2 5-x | 3 | | * | Multiplication | x=4 x*5 | 20 | | / | Division | 15/5 5/2 | 3 2.5 | | % | Modulus (division remainder) | 5%2 10%8 10%2 | 1 2 0 | | ++ | Increment | x=5 x++ | x=6 | | -- | Decrement | x=5 x-- | x=4 |
In the complex expression which is entangled of many operand, operator *, /, and % have the same priority but it is higher rather than operator + and -. For further information, just pay attention to the example as following: $a = 20 + 4 % 4
4 % 4 will be done first, so that result is 21 not 6. Pay attention to the example of using arithmetic operator at script as following : Code: <html> <head> <title>Using Arithmetic Operators</title> </head>
<body> <?php
print("Addition: <br>"); printf("9 + 7 = %d <br>", 9 + 7); printf("9.5 + 7 = %f <br>", 9.5 + 7); printf("9.5 + 7.5 = %f <br>", 9.5 + 7.5); printf("-9.5 + 7.5 = %f <br>", -9.5 + 7.5); print("Reduction: <br>"); printf("9 - 7 = %d <br>", 9 - 7); printf("9.5 - 7 = %f <br>", 9.5 - 7); printf("9.5 - 7.5 = %f <br>", 9.5 - 7.5); printf("-9.5 - 7.5 = %f <br>", -9.5 - 7.5); print("Multiplication: <br>"); printf("9 * 7 = %d <br>", 9 * 7); printf("9.5 * 7 = %f <br>", 9.5 * 7); printf("9.5 * 7.5 = %f <br>", 9.5 * 7.5); printf("-9.5 * 7.5 = %f <br>", -9.5 * 7.5); print("Division: <br>"); printf("9 / 7 = %d <br>", 9 / 7); printf("9.5 / 7 = %f <br>", 9.5 / 7); printf("9.5 / 7.5 = %f <br>", 9.5 / 7.5); printf("-9.5 / 7.5 = %f <br>", -9.5 / 7.5); print("Modulus: <br>"); print(" 9 % 7 = "); print(9 % 7 ."<br>"); print("9.5 % 7 = "); print(9.5 % 7 ."<br>"); print("9.5 % 7.5 = "); print( 9.5 % 7.5."<br>"); print("-9.5 % 7.5 = "); print( -9.5 % 7.5."<br>"); print("9.5 % -7.5 = "); print( 9.5 % -7.5."<br>"); print"<br>";
?> </body> </html> An output of this code will be: Addition: 9 + 7 = 16 9.5 + 7 = 16.500000 9.5 + 7.5 = 17.000000 -9.5 + 7.5 = -2.000000 Reduction: 9 - 7 = 2 9.5 - 7 = 2.500000 9.5 - 7.5 = 2.000000 -9.5 - 7.5 = -17.000000 Multiplication: 9 * 7 = 63 9.5 * 7 = 66.500000 9.5 * 7.5 = 71.250000 -9.5 * 7.5 = -71.250000 Division: 9 / 7 = 1 9.5 / 7 = 1.357143 9.5 / 7.5 = 1.266667 -9.5 / 7.5 = -1.266667 Modulus: 9 % 7 = 2 9.5 % 7 = 2 9.5 % 7.5 = 2 -9.5 % 7.5 = -2 9.5 % -7.5 = 2
Example: $x = 100; // which mean x is given by value 100.
The assignment process can also be conducted with the right operand which is in the form of expression. $y = ($x = 100) + 20;
Assignment operator can also be conducted with the combination of aritmatic operator and string operator which enabling you to use the expression and the result becomes its value. Example:
$x = 100; $x += 90; $x = $x + 90; $greeting ="good"; $greeting = "night"; // good night Pay attention that the assignment can copy the original variable into new variable (assignment by value). PHP have supported assignment by reference. Assignment by reference means that some variable which relates in one of same location (is same variable content)
Its syntax: $var= &$OtherVariable. Example:
Code: <html> <head> <title>Using Arithmetic Operator</title> </head>
<body> <?php
$x = "red"; // assignment by reference $y = &$x; // change $x and $y value $y = "black"; // remove $x variable from memory unset ($x); // print $y value ="black" print ($y); ?> </body> </html> An output of this code will be: black
Assignment operator Table: | Operators | Example
| Is the same as...
| | = | x=y | x=y | | += | x+=y | x=x+y | | -= | x-=y | x=x-y | | *= | x*=y | x=x*y | | /= | x/=y | x=x/y | | .= | x.=y | x=x.y | | %= | x%=y | x=x%y |
Pay attention to the example of using assignment operator at script as following:
Code: <html> <head> <title>Using Operators</title> </head>
<body> <h1>Operator Assignment</h1> <?php $x = 15; echo ("\$x = $x <br />"); $x += 1 ; echo ("\$x += 1 = $x <br /><br />"); $x = 7; echo ("\$x = $x <br>"); $x = $x + 2; echo ("\$x = \$x + 2 = $x <br /><br />"); $x = 87; echo ("\$x = $x <br />"); $y = &$x; echo ("\$y = &\$x = $x <br /><br />"); $greeting = "good"; echo ("\$greeting = $greeting <br /><br />"); $greeting .="night"; echo ("\$greeting .= good = $greeting"); ?> </body> </html>
An output of this code will be: Operator Assignment $x = 15 $x += 1 = 16
$x = 7 $x = $x + 2 = 9
$x = 87 $y = &$x = 87
$greeting = good
$greeting .= good = goodnight
Comparison Operators: Operator
| Description
| Example | | == | is equal to | 5==8 returns false | | != | is not equal | 5!=8 returns true | | > | is greater than | 5>8 returns false | | < | is less than | 5<8 returns true | | >= | is greater than or equal to | 5>=8 returns false | | <= | is less than or equal to | 5<=8 returns true |
Logical Operators: Operator
| Description
| Example
| | && | and | x=6 y=3 (x < 10 && y > 1) returns true | | || | or | x=6 y=3 (x==5 || y==5) returns false | | ! | not | x=6 y=3 !(x==y) returns true |
PHP IF-ELSE STATEMENT
|