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

PHP Arithmetic Operators

Arithmetic operators किसी calculator के math buttons हैं: add, subtract, multiply, divide, और एक remainder ढूँढना। PHP इन्हें आपके numbers और variables के साथ sums करने के लिए इस्तेमाल करता है।
Syntax
php
$result = $a + $b;   // addition
$result = $a - $b;   // subtraction
$result = $a * $b;   // multiplication
$result = $a / $b;   // division
$result = $a % $b;   // remainder
$result = $a ** $b;  // power

Addition और Subtraction

+ और - numbers पर वही addition और subtraction करते हैं जिसकी आप उम्मीद करेंगे, और PHP operate करने से पहले automatically numeric-looking strings ("5" + 3) को numbers में convert कर देगा — जो form input के लिए convenient है लेकिन जानने लायक है, क्योंकि यह एक typo को mask कर सकता है जिसने एक non-numeric string produce की।

उदाहरण: Addition and Subtraction

php
<?php
// Print `10 + 5, "\n"` to the output
echo 10 + 5, "\n";
// Print `10 - 5, "\n"` to the output
echo 10 - 5, "\n";
// Print `"5" + 3` to the output
echo "5" + 3;
?>

Multiplication और Division

* दो values multiply करता है, और / उन्हें divide करता है — लेकिन PHP की division result के whole number न होते ही हमेशा एक float return करती है, भले ही दोनों operands integers रहे हों।

10 / 3 3.3333... देता है, dropped remainder वाला integer नहीं, जो उन languages से आने वाले लोगों को surprise करता है जो default रूप से integer division करती हैं।

उदाहरण: Multiplication and Division

php
<?php
// Print `4 * 3, "\n"` to the output
echo 4 * 3, "\n";
// Print `10 / 3` to the output
echo 10 / 3;
?>

Modulo Operator

% दो integers divide करने के बाद बचा हुआ remainder return करता है, जो इसे parity check करने के लिए standard tool बनाता है ($n % 2 === 0 even के लिए) या values की एक fixed range में cycle करने के लिए, जैसे किसी array के अंत से आगे निकलते ही एक index को zero पर wrap करना।

उदाहरण: Modulo Operator

php
<?php
// Declare `$n`, set to `7`
$n = 7;
// Print `$n % 2 === 0 ? "even" : "odd"` to the output
echo $n % 2 === 0 ? "even" : "odd";
?>

Exponentiation Operator

** left-hand number को right-hand वाले की power तक raise करता है — 2 ** 8 256 evaluate होता है। यह इस exact purpose के लिए पुरानी, clunkier pow() function call का PHP का direct replacement है, और formula में ज़्यादा naturally पढ़ता है।

उदाहरण: Exponentiation Operator

php
<?php
// Print `2 ** 8` to the output
echo 2 ** 8;
?>

Increment और Decrement Operators

++ और -- किसी variable से in place exactly 1 add या subtract करते हैं।

Variable से पहले लिखा गया (++$x) पहले value update करता है फिर इसे return करता है; बाद में लिखा गया ($x++) पहले *original* value return करता है और तभी update apply करता है — एक distinction जो तब मायने रखता है जब आप खुद expression का result इस्तेमाल करें।

उदाहरण: Increment and Decrement Operators

php
<?php
$x = 5;
echo ++$x . "\n"; // pre-increment: 6
$y = 5;
echo $y++ . "\n"; // post-increment: 5
echo $y; // now 6
?>
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. यह उम्मीद करना कि 10 / 4 2 देगा, जबकि PHP का / float 2.5 return करता है; whole-number division के लिए intdiv() इस्तेमाल करें।
  2. floats के साथ % इस्तेमाल करना और एक decimal remainder की उम्मीद करना, जबकि % पहले दोनों operands को integers में convert करता है, इसलिए 5.7 % 2 1 देता है।
  3. zero से divide करना, जो PHP 8 में एक value return करने के बजाय एक DivisionByZeroError throw करता है।

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.