Operators in PHP
PHP operators are very similar in different programming languages like
C++, JavaScript, Java. Operator is a symbol that is used to perform any
operation. Operators are used to perform arithmetic or logic operations,
there are many types of operators. 1) Arithmetic operators
2) Comparison operators
3) Logical operators
Arithmetic Operators
Arithmetic operators are used to perform any arithmetic operation (addition, division, subtraction, multiplication). In PHP following operators are used
| Operator | Example | Functionality |
| + | 2+4 | Addition of 2 or more than 2 values |
| - | 8-2 | Substraction of 2 or more than 2 values |
| * | 5*7 | Multiplaction of 2 or more than 2 values |
| / | 18/3 | Division of 2 or more than 2 values |
| % | 76%10 | Modulus of 2 or more than 2 values |
Example
var $val1=35;
var $val2=38;
var $val3;
val3=$val1+$val2;
echo($val1+”+”+$val2+”=”+$val3);
?>
The output of this example is below:
Comparison Operators
Comparison operators are used to compare two or more value. A double equal sign (==) compares two values but A single equal sign sets a value. Comparison operators are used inside conditional statements and evaluate to either true or false. Following comparison operators are used in PHP.
| Operator | Example | Functionality |
| == | a==b | variable a is equal to variable b |
| != | a!=b | variable a is not equal to variable b |
| < | a<b | variable a is less than variable b |
| > | a>b | variable a is greater than to variable b |
| <= | a<=b | variable a is less than or equal to variable b |
| => | a=>b | variable a is greater than or equal to variable b |
<head>
<title>PHP Example</title>
</head>
<body>
<?php
$a= 8;
$b= 3;
$c= 3;
echo $a ==$b,"<br>";
echo $a != $b,"<br>";
echo $a <$b,"<br>";
echo $a > $b,"<br>";
echo $a >=$c,"<br>";
echo $b <=$c,"<br>";
?>
</body>
</html>
Logical Operators
Logical operators are used to evaluate several comparisons, by combining their possible values.
| Operator | Example | Functionality |
| && | (5>3) && (5<7) | Returns true when both conditions are true. |
| || | (5>3)||(5<7) | Returns true when at least one of the two conditions is true. |
| ! | !(5>3) | Denies the value of the expression. |
<title>PHP Example</title>
</head>
<body>
<?php
$a = 8;
$b = 3;
$c = 3;
echo (a ==b) && ($c > $b),"<br>";
echo ($a == $b) || ($b == $c),"<br>";
echo !($b <= $c),"<br>";
?>
</body>
</html>
Note: You can easily understand comparison and logical operators in next chapter conditional statement.
