Who's Online
We have 7 guests onlineBreadcrumbs
| Operators |
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:
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>
?> An output of this code will be: Addition:
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.
|
| 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