← Back to PHP Course | Chapter 3: Operators | Lesson 8 of 8

PHP Operator Precedence

Operator precedence यह rule है कि कौन सा math step पहले होता है, ठीक school जैसे जहाँ multiply करना add करने से पहले आता है। Brackets आपको जो order चाहिए उसे force करने देते हैं।

Arithmetic Operator Precedence

PHP वही math convention follow करता है जो आपने school में सीखी: * और / + और - से पहले evaluate होते हैं, चाहे वे किसी भी order में लिखे गए हों।

2 + 3 * 4 पहले multiplication evaluate करता है और 20 नहीं, 14 देता है — parentheses एक अलग order force करने का इकलौता तरीका हैं।

उदाहरण: Arithmetic Operator Precedence

php
<?php
// Print `2 + 3 * 4` to the output
echo 2 + 3 * 4;
?>

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
<?php
// Declare `$a`, set to `$b = $c = 0`
$a = $b = $c = 0;
// Print "$a $b $c" to the output
echo "$a $b $c";
?>

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
<?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);
?>

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
<?php
// Declare `$result`, set to `false or true`
$result = false or true;
// Print a detailed dump (with types) of `$result`
var_dump($result);
?>

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
<?php
// Print `10 - 3 - 2` to the output
echo 10 - 3 - 2;
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. $a + $b * $c लिखना और left-to-right evaluation की उम्मीद करना, जबकि * tighter bind करता है, इसलिए intended order के लिए parentheses add करें।
  2. assignments में and/or इस्तेमाल करना, जैसे $x = true and false;, जहाँ = पहले bind करता है इसलिए $x true है।
  3. !$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 होता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.