← Back to PHP Course | Chapter 4: Control Flow | Lesson 10 of 14

PHP while Loop

एक while loop soup को तब तक stir करने जैसा है जब तक यह गर्म न हो जाए: यह तब तक एक action दोहराता रहता है जब तक कोई condition अभी भी true है। यह हर round से पहले condition check करता है।
Syntax
php
while (condition) {
    // body
    // update something that affects the condition
}

Simple while Loop

एक while loop body चलाने से *पहले* अपनी condition check करता है, और जब तक condition true रहती है तब तक उस body को फिर से चलाता रहता है। अगर condition बिल्कुल पहली check पर false है, loop body कभी execute ही नहीं होता -- एक बार भी नहीं।

उदाहरण: Simple while Loop

php
<?php
// Declare `$i`, set to `1`
$i = 1;
// Keep looping while `$i <= 5` holds
while ($i <= 5) {
    // Print `$i . "\n"` to the output
    echo $i . "\n";
    $i++;
}
?>

Loop Counters को Decrement करना

किसी while loop के अंदर count down करने का मतलब बस हर pass पर loop variable में add करने के बजाय उससे subtract करना है -- loop mechanics नहीं बदलते, सिर्फ variable जिस direction move करता है, और उसी हिसाब से जिस condition के against आप check करते हैं।

उदाहरण: Decrementing Loop Counters

php
<?php
// Declare `$i`, set to `5`
$i = 5;
// Keep looping while `$i > 0` holds
while ($i > 0) {
    // Print `$i . "\n"` to the output
    echo $i . "\n";
    $i--;
}
?>

Boolean Flags के साथ Loops

एक boolean flag को loop की condition की तरह इस्तेमाल करना loop body के *अंदर* के code को बस उस flag को false set करके तय करने देता है कि कब रुकना है -- यह तब उपयोगी है जब stopping condition पहले से जानी गई किसी value के बजाय किसी iteration के बीच में discover हुई किसी चीज़ पर depend करती हो।

उदाहरण: Loops with Boolean Flags

php
<?php
// Declare `$keepGoing`, set to `true`
$keepGoing = true;
// Declare `$count`, set to `0`
$count = 0;
// Keep looping while `$keepGoing` holds
while ($keepGoing) {
    $count++;
    // Print `$count . "\n"` to the output
    echo $count . "\n";
    // Check whether `$count == 3`
    if ($count == 3) {
        // Declare `$keepGoing`, set to `false`
        $keepGoing = false;
    }
}
?>

Alternative while Syntax

colon-based alternative syntax (while (...): ... endwhile;) brace form जैसा ही identically behave करता है लेकिन तब ज़्यादा cleanly पढ़ता है जब एक loop किसी template file में सीधे HTML markup के साथ interleaved हो।

उदाहरण: Alternative while Syntax

php
<?php $i = 1; ?>
<?php // Keep looping and printing while $i is 3 or less, incrementing each time ?>
<?php while ($i <= 3): ?>
  <p>Item <?php echo $i; ?></p>
  <?php $i++; ?>
<?php endwhile; ?>

Mathematical Loops

हर iteration पर किसी variable को multiply या halve करना -- fixed amount add या subtract करने के बजाय -- exponential growth या decay produce करता है, जो capacity double करने या किसी search range को बार-बार halve करने जैसी चीज़ों के पीछे का pattern है।

उदाहरण: Mathematical Loops

php
<?php
// Declare `$value`, set to `1`
$value = 1;
// Keep looping while `$value < 100` holds
while ($value < 100) {
    // Print `$value . "\n"` to the output
    echo $value . "\n";
    $value *= 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. body के अंदर loop variable update करना भूल जाना, एक infinite loop बनाते हुए।
  2. array counts के साथ < के बजाय <= इस्तेमाल करना, जो एक iteration ज़्यादा चलाता है और एक undefined index पढ़ता है।
  3. यह मान लेना कि body कम से कम एक बार चलता है, जबकि while पहले condition check करता है, इसलिए यह कभी न चले।

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.