Table of Contents
PHP operators introduction
हेलो दोस्तों इस post tutorial में सीखेंगे की php operators का उपयोग करके हम variable और values के लिए operation perform कैसे कर सकते है।।
अगर हम two values 10,15 का addition 10+15=25; ,substraction , multiplication , division करना या comparision , greterthan , lessthan statement check करने के लिए operators का इस्तेमाल किया जाता है||
PHP Operators
- Arithmetic operators
- Assignment operators
- Logical operators
- Comparision operators
- Increment/decrement operators
- Conditional operators
- String operators
Arithmetic operators
php में arithmetic operator का इस्तेमाल करके numeric values का adition , substraction , multiplication जैसे arithmetic operation perform करा सकते है||
Arithmetic all operator description:
Operator | Description | Example | Result |
+ | two value addition | 10 + 2 | 12 |
– | subtraction operation | 10 – 2 | 8 |
* | multiplication operation | 10 * 2 | 20 |
/ | Division operation | 10 / 2 | 5 |
Example:
<?php
/* adition operation */
echo "Adition is :". (10 + 2); echo "<br>";
//output is : 12
/* substraction operation */
echo "Substraction is :". (10 - 2);echo "<br>";
//output is : 8
/* multiplication operation */
echo "Multiplication is :". (10 * 2);echo "<br>";
//output is : 20
/* Division operation */
echo "Division is :". (10 / 2);echo "<br>";
//output is : 5
/* modulus operation */
echo "Modulus is :". (10 % 2);echo "<br>";
//output is : 0
/* exponentiation operation */
echo "Exponentiation is :".(10 ** 2);
//output is : 100
?>
output:
Adition is :12
Substraction is :8
Multiplication is :20
Division is :5
Modulus is :0
Exponentiation is :100
Assignment operators
assignment operator का इस्तेमाल variable में value को asign करने के लिए होता है| php में value को variable में asing करने के लिए ” = ” sign का इस्तेमाल होता है|
Operator | Description |
$a = $b | assign |
$a += $b | add and assign |
$a -= $b | subtraction and assign |
$a *= $b | multiplication and assign |
$a /= $b | division and assign |
$a %= $b | module and assign |
Example:
<?php
$a = 50; // asign
echo $a; echo "<br>";
//output is : 50;
$a += 10; // add and asign
echo $a; echo "<br>";
//output is : 60
$a -= 10; //substraction and asign
echo $a; echo "<br>";
//output is : 40
$a *= 10; //multiplication and asign
echo $a; echo "<br>";
//output is : 500
$a /= 10; //division and asign
echo $a; echo "<br>";
//output is : 5
$a %= 10; //modulus and asign
echo $a;
//output is : 5
?>
Comparision operators
php में अगर दो variable values का comparision करना होतो comparision operator का इस्तेमाल होता है||
Operator | Description | Example |
== | equal | $a == $b |
=== | identical equal | $a===$b |
!= | not equal | $a!=$b |
<> | not equal | $a<>$b |
!== | not identical equal | $a!==$b |
< | less than | $a<$b |
> | greater than | $a>$b |
>= | greater than or equal | $a>=$b |
<= | less than or equal | $a<=$b |
Example:
<?php
$a = 8;
$b = 5;
$c = "8";
var_dump($a == $c); echo "<br>"; //return true
var_dump($a != $b); echo "<br>"; // true
var_dump($a <> $b); echo "<br>"; // true
var_dump($a === $c); echo "<br>"; // false
var_dump($a !== $c); echo "<bt>"; // true
var_dump($a < $b); echo "<bt>"; // false
var_dump($a > $b); echo "<bt>"; // true
var_dump($a <= $b); echo "<bt>"; // false
var_dump($a >= $b); // true
?>
Increment/decrement operators
php में variable value का increment and decrement operation perform करने के लिए यह operators का इस्तेमाल होता है||
Operator | Description |
++$a | $a में एक increment होने के बाद $a return करता है| |
$a++ | $a पहले return होने के बाद $a में एक increment होता है| |
–$a | $a में एक decrement होने के बाद $a return करता है| |
$a– | $a पहले return होने के बाद $a में एक decrement होता है| |
Example:
<?php
$a=5;
echo ++$a;
//output is : 6
echo $a++;
//output is : 5
echo --$a;
//output is : 4
echo $a--;
//output is : 5
?>
Logical operators
php में एक साथ दो और इससे ज्यादा conditional statements perform या check करना होतो इसके लिए logical operator का इस्तेमाल होता है|
Operator | Description | Example |
&& | And operator | $a && $b |
|| | OR operator | $a || $b |
! | NOT operator | ! $a |
and | And operator | $a and $b |
or | OR operator | $a OR $b |
xor | XOR operator | $a XOR $b |
Example:
<?php
$a = 10;
$b = 5;
$c = true;
if ($a == 10 and $b == 5)
echo "and operator Success <br>";
if ($a == 10 or $b == 5)
echo "or operator Success <br>";
if ($a == 10 xor $b == 5)
echo "xor operator Success <br>";
if ($a == 10 && $b == 5)
echo "&& operator Success <br>";
if ($a == 10 || $b == 5)
echo "|| operator Success <br>";
if (!$c)
echo "! operator Success \n";
?>
String operators
string operator specially string के लिए इस्तेमाल होते है|
Operator | Description | Example |
. | Concatenation | $a.$b |
.= | Concatenation and asign | $a.=$b |
Example:
<?php
$a = "hello guys";
$b = "this is RjtechyG";
echo $a.$b;
//output is : hello guys this is RjtechyG
echo $a.=$b;
//output is : hello guys this is RjtechyG
?>
Conditional operators
condition operator का इस्तेमाल दो variable value को compare करने के लिए होता है इस का result TRUE या FALSE पर निर्भर करता है|
यह operator if…else statement जैसे same काम करता है तो हम if…else के option में इस्तेमाल कर सकते है|
Operator | Description | Example |
? : | Concatenation | $a==$b ? true : false; |
Example:
<?php
$a = -1;
echo ($a > 0) ? ' This is positive number ' : ' This is negative number ';
?>
output:
This is negative number