Operators in PHP

Operators in PHP

Operators का उपयोग कुछ Values पर Operations करने के लिए किया जाता है। दूसरे शब्दों में, हम Operators को describe कुछ ऐसा कर सकते हैं जो कुछ Value लेता है, उन पर कुछ Operation करता है और Result देता है। उदाहरण से, “1 + 2 = 3” इस expression में ‘+’ एक Operator है। यह दो मान 1 और 2 लेता है, 3 देने के लिए उन पर addition operation करता है।
किसी भी अन्य Programming language की तरह, PHP भी विभिन्न प्रकार के Operation को Support करता है जैसे Arithmetic Operation (addition, Subtraction, etc), logical operation (And, OR etc), Increment/Decrement Operations आदि। इस प्रकार, PHP हमें कई Operators Provide करता है। विभिन्न Operand या Variable या Values पर इस तरह के Operation करें। ये Operators और कुछ नहीं बल्कि विभिन्न प्रकार के Operations करने के लिए आवश्यक Symbols हैं। नीचे Operators के different Group दिए गए हैं:

  • Arithmetic Operators
  • Logical or Relational Operators
  • Comparison Operators
  • Conditional or Ternary Operators
  • Assignment Operators
  • Spaceship Operators (Introduced in PHP 7)
  • Array Operators
  • Increment/Decrement Operators
  • String Operators

आइए अब हम इनमें से प्रत्येक Operator के बारे में Details में जानें:

Arithmetic Operators

Arithmetic Operators का उपयोग Addition, Subtraction, Multiplication आदि जैसे simple mathematical operations को करने के लिए किया जाता है। नीचे Arithmetic Operators की list के साथ-साथ Syntax और Operation है, जो PHP हमें प्रदान करता है:

Operator Name Syntax Operation
+ Addition $x + $y Sum the operands
Subtraction $x-$y Differences the operands
* Multiplication $x*$y Product of the operands
/ Division $x/$y Quotient of the operands
** Exponentiation $x ** $y $x raised to the power $y
% Modulus $x%$y Remainder of the operands

Example

<?php

// variable 1
$x = 29;

// variable 2
$y = 4;

// some arithmetic operations on
// these two variables
echo ($x + $y), "\n";
echo($x - $y), "\n";
echo($x * $y), "\n";
echo($x / $y), "\n";
echo($x % $y), "\n";

?>

Output

33
25
116
7.25
1

Logical or Relational Operators

ये मूल रूप से Conditional Statements और expressions के साथ काम करने के लिए उपयोग किए जाते हैं। Conditional statements Conditions  पर based हैं। इसके अलावा, एक Condition या तो पूरी की जा सकती है या पूरी नहीं की जा सकती है, इसलिए एक Conditional Statements का Result या तो सही या गलत हो सकता है। यहाँ Syntax और operations के साथ logical operators हैं, जो PHP हमें प्रदान करता है:

Operator Name Syntax Operation
and Logical AND $x and $y True if both the operands are true else false
or Logical OR $x or $y True if either of the operand is true else false
xor Logical XOR $x xor $y True if either of the operand is true and false if both are true
&& Logical AND $x && $y True if both the operands are true else false
|| Logical OR $x || $y True if either of the operand is true else false
! Logical NOT !$x True if $x is false

Example

<?php

$x = 50;
$y = 30;

if ($x == 50 and $y == 30)
    echo "and Success \n";

if ($x == 50 or $y == 20)
    echo "or Success \n";

if ($x == 50 xor $y == 20)
    echo "xor Success \n";

if ($x == 50 && $y == 30)
    echo "&& Success \n";

if ($x == 50 || $y == 20)
    echo "|| Success \n";

if (!$z)
    echo "! Success \n";

?>

Output

and Success or Success xor Success && Success || Success ! Success

Comparison Operators

इन Operators का उपयोग दो elements की तुलना करने के लिए किया जाता है और Result को Boolean Form में Output करता है। यहाँ Syntax और Operation के साथ Comparison Operators हैं, जो PHP हमें प्रदान करता है:

Operator Name Syntax Operation
== Equal To $x == $y Returns True if both the operands are equal
!= Not Equal To $x != $y Returns True if both the operands are not equal
<> Not Equal To $x <> $y Returns True if both the operands are unequal
=== Identical $x === $y Returns True if both the operands are equal and are of the same type
!== Not Identical $x == $y Returns True if both the operands are unequal and are of different types
< Less Than $x < $y Returns True if $x is less than $y
> Greater Than $x > $y Returns True if $x is greater than $y
<= Less Than or Equal To $x <= $y Returns True if $x is less than or equal to $y
>= Greater Than or Equal To $x >= $y Returns True if $x is greater than or equal to $y

Example

<?php

$a = 80;
$b = 50;
$c = "80";

// Here var_dump function has been used to
// display structured information. We will learn
// about this function in complete details in further
// articles.
var_dump($a == $c) + "\n";
var_dump($a != $b) + "\n";
var_dump($a <> $b) + "\n";
var_dump($a === $c) + "\n";
var_dump($a !== $c) + "\n";
var_dump($a < $b) + "\n";
var_dump($a > $b) + "\n";
var_dump($a <= $b) + "\n";
var_dump($a >= $b);

?>

Output

bool(true)
bool(true)
bool(true)
bool(false)
bool(true)
bool(false)
bool(true)
bool(false)
bool(true)

Conditional or Ternary Operators

इन Operators का उपयोग दो Values की तुलना करने और Result के True या False होने के आधार पर एक साथ Result लेने के लिए किया जाता है। इन्हें if…else statement  के लिए shorthand notation के रूप में भी उपयोग किया जाता है जिसे हम decision लेने पर Article में पढ़ेंगे।

Syntax

$var = (condition)? value1 : value2;

यहां, स्थिति या तो सही या गलत का मूल्यांकन करेगी। यदि शर्त का मूल्यांकन सही है, तो मान 1 को वैरिएबल $var को असाइन किया जाएगा अन्यथा value2 को इसे असाइन किया जाएगा।

Operator Name Operation
?: Ternary If condition is true ? then $x : or else $y. This means that if condition is true then left result of the colon is accepted otherwise the result on right.
<?php

$x = -12;

echo ($x > 0) ? 'The number is positive' : 'The number is negative';

?>

Output

The number is negative

Assignment Operators

इन Operators का उपयोग mid-operations के साथ या बिना विभिन्न Variable के लिए assign values करने के लिए किया जाता है। यहाँ Syntax और operations के साथ Assignment operators हैं, जो PHP हमें प्रदान करता है:

Operator Name Syntax Operation
= Assign $x = $y Operand on the left obtains the value of the operand on right
+= Add then Assign $x += $y Simple Addition same as $x = $x + $y
-= Subtract then Assign $x -= $y Simple subtraction same as $x = $x – $y
*= Multiply then Assign $x *= $y Simple product same as $x = $x * $y
/= Divide then Assign (quotient) $x /= $y Simple division same as $x = $x / $y
%= Divide then Assign (remainder) $x %= $y Simple division same as $x = $x % $y
<?php

// simple assign operator
$y = 75;
echo $y, "\n";

// add then assign operator
$y = 100;
$y += 200;
echo $y, "\n";

// subtract then assign operator
$y = 70;
$y -= 10;
echo $y, "\n";

// multiply then assign operator
$y = 30;
$y *= 20;
echo $y, "\n";

// Divide then assign(quotient) operator
$y = 100;
$y /= 5;
echo $y, "\n";

// Divide then assign(remainder) operator
$y = 50;
$y %= 5;
echo $y;

?>

Output

75
300
60
600
20
0

Array Operators

इन Operators का उपयोग सरणियों के मामले में किया जाता है। यहाँ Syntax और operations के साथ-साथ array operator हैं, जो PHP हमें प्रदान करता है:

Operator Name Syntax Operation
+ Union $x + $y Union of both i.e., $x and $y
== Equality $x == $y Returns true if both has same key-value pair
!= Inequality $x != $y Returns True if both are unequal
=== Identity $x === $y Returns True if both has same key-value pair in the same order and of same type
!== Non-Identity $x !== $y Returns True if both are not identical to each other
<> Inequality $x <> $y Returns True if both are unequal

Example

<?php

$x = array("k" => "Car", "l" => "Bike");
$y = array("a" => "Train", "b" => "Plane");

var_dump($x + $y);
var_dump($x == $y) + "\n";
var_dump($x != $y) + "\n";
var_dump($x <> $y) + "\n";
var_dump($x === $y) + "\n";
var_dump($x !== $y) + "\n";

?>

Output

array(4) { [“k”]=> string(3) “Car” [“l”]=> string(4) “Bike” [“a”]=> string(5) “Train” [“b”]=> string(5) “Plane” } bool(false) bool(true) bool(true) bool(false) bool(true)

Increment/Decrement Operators

इन्हें Unary Operator कहा जाता है क्योंकि यह Single Operand पर काम करते हैं। इनका उपयोग Values को Increment या decrement के लिए किया जाता है।

Operator Name Syntax Operation
++ Pre-Increment ++$x First increments $x by one, then return $x
Pre-Decrement –$x First decrements $x by one, then return $x
++ Post-Increment $x++ First returns $x, then increment it by one
Post-Decrement $x– First returns $x, then decrement it by one

Example

<?php

$x = 2;
echo ++$x, " First increments then prints \n";
echo $x, "\n";

$x = 2;
echo $x++, " First prints then increments \n";
echo $x, "\n";

$x = 2;
echo --$x, " First decrements then prints \n";
echo $x, "\n";

$x = 2;
echo $x--, " First prints then decrements \n";
echo $x;

?>

Output

3 First increments then prints 
3
2 First prints then increments 
3
1 First decrements then prints 
1
2 First prints then decrements

String Operators

इन्हें Strings पर Apply किया जाता है।

Operator Name Syntax Operation
. Concatenation $x.$y Concatenated $x and $y
.= Concatenation and assignment $x.=$y First concatenates then assigns, same as $x = $x.$y
<?php

$x = "Geeks";
$y = "for";
$z = "Geeks!!!";
echo $x . $y . $z, "\n";

$x .= $y . $z;
echo $x;

?>

Spaceship Operators (Introduced in PHP 7)

PHP 7 ने एक नए प्रकार का Operator पेश किया है जिसे Spaceship Operator () कहा जाता है। इन Operators का उपयोग Values की तुलना करने के लिए किया जाता है, लेकिन Boolean results return करने के बजाय, यह integer value देता है। यदि दोनों operand equal हैं, तो यह 0 देता है। यदि right operand बड़ा है, तो यह -1 देता है। यदि right operand बड़ा है, तो यह 1 return करता है। निम्न तालिका दिखाती है कि यह details में कैसे काम करता है:

Operator Syntax Operation
$x < $y $x <=> $y Identical to -1 (right is greater)
$x > $y $x <=> $y Identical to 1 (left is greater)
$x <= $y $x <=> $y Identical to -1 (right is greater) or identical to 0 (if both are equal)
$x >= $y $x <=> $y Identical to 1 (if left is greater) or identical to 0 (if both are equal)
$x == $y $x <=> $y Identical to 0 (both are equal)
$x != $y $x <=> $y Not Identical to 0
<?php

$x = 50;
$y = 50;
$z = 25;

echo $x <=> $y;
echo "\n";

echo $x <=> $z;
echo "\n";

echo $z <=> $y;
echo "\n";

// We can do the same for Strings
$x = "Ram";
$y = "Krishna";

echo $x <=> $y;
echo "\n";

echo $x <=> $y;
echo "\n";

echo $y <=> $x;

?>

Output

0 
1 
-1 
1 
1 
-1

 

Previous articleDecision Making Statements in PHP
Next articleIntroduction to Bluetooth Architecture and Application

LEAVE A REPLY

Please enter your comment!
Please enter your name here