PHP Increment और Decrement
In this page:
$x++; // post-increment: use value, then add 1
++$x; // pre-increment: add 1, then use value
$x--; // post-decrement
--$x; // pre-decrement
Post-Increment Operator
$x++ (post-increment) पहले $x की current value पढ़ता है, और *उस read के बाद* ही PHP variable में 1 add करता है।
अगर आप expression का result सीधे इस्तेमाल करते हैं, आपको increment से पहले की value मिलती है — update होता है, बस उस particular expression के लिए invisibly।
उदाहरण: Post-Increment Operator
<?php
// Declare `$x`, set to `5`
$x = 5;
// Print `$x++` to the output
echo $x++;
// Print "\n" to the output
echo "\n";
// Print `$x` to the output
echo $x;
?>
Login to try C/C++/Java/PHP code in the editor
Pre-Increment Operator
++$x (pre-increment) पहले variable में 1 add करता है, फिर पहले से updated value return करता है।
आप pre- या post-increment इस्तेमाल करते हैं यह सिर्फ तब मायने रखता है जब आप उसी line में कहीं और expression के *result* का इस्तेमाल करें — एक standalone statement की तरह, $x++ और ++$x एक जैसे behave करते हैं।
उदाहरण: Pre-Increment Operator
<?php
// Declare `$x`, set to `5`
$x = 5;
// Print `++$x` to the output
echo ++$x;
// Print "\n" to the output
echo "\n";
// Print `$x` to the output
echo $x;
?>
Login to try C/C++/Java/PHP code in the editor
Post-Decrement Operator
$x-- (post-decrement) post-increment को mirror करता है: यह current value return करता है, फिर बाद में 1 subtract करता है। यह एक countdown loop के लिए natural choice है जहाँ आप उस iteration पर value घटने *से पहले* इस पर act करना चाहते हैं।
उदाहरण: Post-Decrement Operator
<?php
// Declare `$x`, set to `5`
$x = 5;
// Print `$x--` to the output
echo $x--;
// Print "\n" to the output
echo "\n";
// Print `$x` to the output
echo $x;
?>
Login to try C/C++/Java/PHP code in the editor
Pre-Decrement Operator
--$x (pre-decrement) पहले 1 subtract करता है और नई, पहले से कम value return करता है।
pre-increment की तरह, post- version से अंतर सिर्फ तब दिखता है जब expression की return value को standalone statement के बजाय असल में consume किया जाए।
उदाहरण: Pre-Decrement Operator
<?php
// Declare `$x`, set to `5`
$x = 5;
// Print `--$x` to the output
echo --$x;
// Print "\n" to the output
echo "\n";
// Print `$x` to the output
echo $x;
?>
Login to try C/C++/Java/PHP code in the editor
Characters को Increment करना
PHP के पास एक असामान्य extra ability है: letters वाली एक string को increment करना इसे alphabetically आगे बढ़ाता है — $letter = a; $letter++; b produce करता है, और z को increment करना aa में roll over होता है, ठीक वैसे जैसे एक car odometer एक digit carry over करता है।
उदाहरण: Incrementing Characters
<?php
// Declare `$letter`, set to 'a'
$letter = 'a';
$letter++;
// Print `$letter . "\n"` to the output
echo $letter . "\n";
// Declare `$letter`, set to 'z'
$letter = 'z';
$letter++;
// Print `$letter` to the output
echo $letter;
?>
Login to try C/C++/Java/PHP code in the editor
$x = $x++;इस्तेमाल करना, जहाँ old value वापस assign हो जाती है और increment खो जाता है।$a[$i++]जैसे बड़े expressions के अंदर$i++और++$iको mix up करना, ताकि इस्तेमाल की गई value एक से अलग हो।- यह उम्मीद करना कि
++nullपर numbers जैसे काम करेगा, जबकिnullको increment करना1देता है लेकिन इसे decrement करनाnullही छोड़ देता है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: