Operators का उपयोग Data को Developed करने के लिए किया जाता है. Programming Languages मे Operators को Mathematics से लिया जाता है और Operator Programmers Data के साथ काम करते है.
Operator एक Symbols की एक Chain है जो कुछ Values के साथ प्रयोग किया जाता है. Variables और Values पर संचालन करने के लिए हम Operators का उपयोग करते है.
PHP भाषा बहुत से प्रकार के Operators का समर्थन करती है.
Arithmetic Operators
Comparison Operators
Assignment Operators
Logical Operators
Conditional Operators
Arithmetic Operators को variables के साथ Arithmetic Operations Perform करने के लिए उपयोग किया जाता है. PHP भाषा मे इन + , - , * , / , % , ++ , -- Arithmetic Operators का उपयोग किया जाता है.
Operatos | Description |
---|---|
+ | Add |
- | Subtract |
* | Multiply |
/ | Divide and return quotient |
% | Divide and return modulus |
<!DOCTYPE html>
<html>
<head>
<title>PHP Arithmetic Operators Example</title>
</head>
<body>
<?php
$var1 = 28;
$var2 = 78;
$res = $var1 + $var2;
echo "Addtion Result is $res <br/>";
$res = $var2 - $var1;
echo "Substraction Result is $res <br/>";
$res = $var1 * $var2;
echo "Multiplication Result is $res <br/>";
$res = $var1 / $var2;
echo "Division Result is $res <br/>";
$res = $var1 % $var2;
echo "Modulus Result is $res <br/>";
$res = $var1++;
echo "Increment Result is $res <br/>";
$res = $var1--;
echo "Decrement Result is $res <br/>";
?>
</body>
</html>
Comparison Operators दो Variables की Values को Compare करने के लिए उपयोग किया जाता है. PHP भाषा मे इन == , != , > , < , >= , <= Comparison Operators का उपयोग किया जाता है.
Operatos | Description |
---|---|
== | Equal to |
=== | Equal to and of the same type |
!= | Not equal to |
!== | Not equal to and of the same type |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
<!DOCTYPE html>
<html>
<head>
<title>PHP Comparison Operators Example</title>
</head>
<body>
<?php
$a = 65;
$b = 85;
if( $a == $b ){
echo "a is equal to b <br/>";
}
else{
echo "a is not equal to b <br/>";
}
if( $a > $b ){
echo "a is greater than b <br/>";
}
else{
echo "a is not greater than b <br/>";
}
if( $a < $b ){
echo "a is less than b <br/>";
}
else{
echo "a is not less than b <br/>";
}
if( $a != $b ){
echo "a is not equal to b <br/>";
}
else{
echo "a is equal to b <br/>";
}
if( $a >= $b ){
echo "a is either greater than or equal to b <br/>";
}
else{
echo "a is neither greater than nor equal to b <br/>";
}
if( $a <= $b ){
echo "a is either less than or equal to b <br/>";
}
else{
echo "a is neither less than nor equal to b <br/>";
}
?>
</body>
</html>
Assignment Operators को एक Variable में Value लिखने के लिए Numeric Values के साथ उपयोग किया जाता है. PHP भाषा मे इन = , += , -= , *= , /= , %= Assignment Operators का उपयोग किया जाता है.
Operatos | Description |
---|---|
+= | Add and assign |
-= | Subtract and assign |
*= | Multiply and assign |
/= | Divide and assign quotient |
%= | Divide and assign modulus |
.= | Concatenate and assign |
<!DOCTYPE html>
<html>
<head>
<title>PHP Assignment Operators Example</title>
</head>
<body>
<?php
$a = 35;
$b = 50;
$res = $a + $b;
echo "Addtion Operation Result: $res <br/>";
$res += $a;
echo "Add and Assigment Operation Result: $res <br/>";
$res -= $a;
echo "Subtract and Assignment Operation Result: $res <br/>";
$res *= $a;
echo "Multiply and Assignment Operation Result: $res <br/>";
$res /= $a;
echo "Division and Assignment Operation Result: $res <br/>";
$res %= $a;
echo "Modulus and Assignment Operation Result: $res <br/>";
?>
</body>
</html>
Logical Operators Logic Perform करने के लिए उपयोग किया जाता है और ये Operators Control Statements में भी प्रयोग किया जाता है. PHP भाषा मे इन and , or , && , || , ! Logical Operators का उपयोग किया जाता है.
Operatos | Description |
---|---|
&& | And |
|| | Or |
! | Not |
<!DOCTYPE html>
<html>
<head>
<title>PHP Logical Operators Example</title>
</head>
<body>
<?php
$a = 0;
$b = 21;
if( $a && $b ){
echo "Both a and b are true<br/>";
}
else{
echo "Either a or b is false<br/>";
}
if( $a and $b ){
echo "Both a and b are true<br/>";
}
else{
echo "Either a or b is false<br/>";
}
if( $a || $b ){
echo "Either a or b is true<br/>";
}
else{
echo "Both a and b are false<br/>";
}
if( $a or $b ){
echo "Either a or b is true<br/>";
}
else{
echo "Both a and b are false<br/>";
}
if( !$a ){
echo "a is true <br/>";
}
else{
echo "a is false<br/>";
}
?>
</body>
</html>
PHP भाषा मे इन " ?: " (If Condition is true ? Then true value : Otherwise false value) Conditional Operators का उपयोग किया जाता है. Conditional Operators को Ternary Operators भी कहा जाता है.
<!DOCTYPE html>
<html>
<head>
<title>PHP Conditional/Ternary Operator Example</title>
</head>
<body>
<?php
$a = 50;
$b = 35;
$res = ($a > $b ) ? $a ." is greater than ". $b : $b." is greater than ". $a;
echo "The result :: $res";
?>
</body>
</html>