PHP Arithmetic Operators
In this page:
$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
// 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;
?>
Login to try C/C++/Java/PHP code in the editor
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
// Print `4 * 3, "\n"` to the output
echo 4 * 3, "\n";
// Print `10 / 3` to the output
echo 10 / 3;
?>
Login to try C/C++/Java/PHP code in the editor
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
// Declare `$n`, set to `7`
$n = 7;
// Print `$n % 2 === 0 ? "even" : "odd"` to the output
echo $n % 2 === 0 ? "even" : "odd";
?>
Login to try C/C++/Java/PHP code in the editor
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
// Print `2 ** 8` to the output
echo 2 ** 8;
?>
Login to try C/C++/Java/PHP code in the editor
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
$x = 5;
echo ++$x . "\n"; // pre-increment: 6
$y = 5;
echo $y++ . "\n"; // post-increment: 5
echo $y; // now 6
?>
Login to try C/C++/Java/PHP code in the editor
- यह उम्मीद करना कि
10 / 42देगा, जबकि PHP का/float2.5return करता है; whole-number division के लिएintdiv()इस्तेमाल करें। - floats के साथ
%इस्तेमाल करना और एक decimal remainder की उम्मीद करना, जबकि%पहले दोनों operands को integers में convert करता है, इसलिए5.7 % 21देता है। - zero से divide करना, जो PHP 8 में एक value return करने के बजाय एक
DivisionByZeroErrorthrow करता है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: