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

PHP Increment और Decrement

Increment और decrement एक जोड़ने या एक घटाने के shortcuts हैं, किसी tally counter को click करने जैसे। यह मायने रखता है कि PHP number देने से पहले count करता है या बाद में।
Syntax
php
$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
<?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;
?>

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

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

Pre-Decrement Operator

--$x (pre-decrement) पहले 1 subtract करता है और नई, पहले से कम value return करता है।

pre-increment की तरह, post- version से अंतर सिर्फ तब दिखता है जब expression की return value को standalone statement के बजाय असल में consume किया जाए।

उदाहरण: Pre-Decrement Operator

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

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
<?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;
?>
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. $x = $x++; इस्तेमाल करना, जहाँ old value वापस assign हो जाती है और increment खो जाता है।
  2. $a[$i++] जैसे बड़े expressions के अंदर $i++ और ++$i को mix up करना, ताकि इस्तेमाल की गई value एक से अलग हो।
  3. यह उम्मीद करना कि ++ null पर numbers जैसे काम करेगा, जबकि null को increment करना 1 देता है लेकिन इसे decrement करना null ही छोड़ देता है।

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.