PHP Operator Precedence
In this page:
Arithmetic Operator Precedence
PHP वही math convention follow करता है जो आपने school में सीखी: * और / + और - से पहले evaluate होते हैं, चाहे वे किसी भी order में लिखे गए हों।
2 + 3 * 4 पहले multiplication evaluate करता है और 20 नहीं, 14 देता है — parentheses एक अलग order force करने का इकलौता तरीका हैं।
उदाहरण: Arithmetic Operator Precedence
<?php
// Print `2 + 3 * 4` to the output
echo 2 + 3 * 4;
?>
Login to try C/C++/Java/PHP code in the editor
Assignment Operator Precedence
Assignment operators PHP की precedence table के bottom के पास बैठते हैं और right-to-left group होते हैं, जो exactly वह चीज़ है जो $a = $b = $c = 0; जैसी chain को उम्मीद के मुताबिक काम करवाती है — $c पहले assign होता है, फिर वही value left की ओर $b में, फिर $a में flow करती है।
उदाहरण: Assignment Operator Precedence
<?php
// Declare `$a`, set to `$b = $c = 0`
$a = $b = $c = 0;
// Print "$a $b $c" to the output
echo "$a $b $c";
?>
Login to try C/C++/Java/PHP code in the editor
Logical Operator Precedence
Comparison operators (>, ==, वगैरह) logical operators (&&, ||) से tighter bind करते हैं, इसलिए $age > 18 && $hasId explicit parentheses की ज़रूरत के बिना ($age > 18) && $hasId की तरह parse होता है — comparisons पहले booleans में resolve होती हैं, और तभी logical operator उन्हें combine करता है।
उदाहरण: Logical Operator Precedence
<?php
// Declare `$age`, set to `20`
$age = 20;
// Declare `$hasId`, set to `true`
$hasId = true;
// Print a detailed dump (with types) of `$age > 18 && $hasId`
var_dump($age > 18 && $hasId);
?>
Login to try C/C++/Java/PHP code in the editor
and बनाम && Precedence
word-form logical operators (and, or) की precedence उनके symbolic counterparts (&&, ||) से काफ़ी *कम* है — इतनी कम कि $result = false or true; $result में false assign करता है, क्योंकि = or से tighter bind करता है।
यही exact gotcha है जिसकी वजह से ज़्यादातर style guides लोगों को expressions में &&/|| की ओर steer करती हैं।
उदाहरण: and vs && Precedence
<?php
// Declare `$result`, set to `false or true`
$result = false or true;
// Print a detailed dump (with types) of `$result`
var_dump($result);
?>
Login to try C/C++/Java/PHP code in the editor
Left-to-Right Associativity
जब दो operators same precedence level share करते हैं, PHP उन्हें left से right resolve करता है — 10 - 3 - 2 (10 - 3) - 2 की तरह evaluate होता है, 5 देते हुए, 10 - (3 - 2) नहीं।
यह left-to-right rule वह है जिस पर आप तब भरोसा करते हैं जब सिर्फ precedence order पूरी तरह decide नहीं कर पाती।
उदाहरण: Left-to-Right Associativity
<?php
// Print `10 - 3 - 2` to the output
echo 10 - 3 - 2;
?>
Login to try C/C++/Java/PHP code in the editor
$a + $b * $cलिखना और left-to-right evaluation की उम्मीद करना, जबकि*tighter bind करता है, इसलिए intended order के लिए parentheses add करें।- assignments में
and/orइस्तेमाल करना, जैसे$x = true and false;, जहाँ=पहले bind करता है इसलिए$xtrueहै। !$a instanceof Bलिखना और उम्मीद करना कि यहinstanceofका result negate कर देगा, जबकिinstanceof!से tighter bind करता है, हालाँकि parentheses के साथ यह clearer है।
- PHP में arithmetic, relational, logical, bitwise, और assignment operators हैं।
- Increment, decrement, और ternary operator values update करने या एक result choose करने के छोटे तरीके देते हैं।
- Operator precedence decide करता है कि एक mixed expression कैसे evaluate होता है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: