PHP while Loop
In this page:
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
// 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++;
}
?>
Login to try C/C++/Java/PHP code in the editor
Loop Counters को Decrement करना
किसी while loop के अंदर count down करने का मतलब बस हर pass पर loop variable में add करने के बजाय उससे subtract करना है -- loop mechanics नहीं बदलते, सिर्फ variable जिस direction move करता है, और उसी हिसाब से जिस condition के against आप check करते हैं।
उदाहरण: Decrementing Loop Counters
<?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--;
}
?>
Login to try C/C++/Java/PHP code in the editor
Boolean Flags के साथ Loops
एक boolean flag को loop की condition की तरह इस्तेमाल करना loop body के *अंदर* के code को बस उस flag को false set करके तय करने देता है कि कब रुकना है -- यह तब उपयोगी है जब stopping condition पहले से जानी गई किसी value के बजाय किसी iteration के बीच में discover हुई किसी चीज़ पर depend करती हो।
उदाहरण: Loops with Boolean Flags
<?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;
}
}
?>
Login to try C/C++/Java/PHP code in the editor
Alternative while Syntax
colon-based alternative syntax (while (...): ... endwhile;) brace form जैसा ही identically behave करता है लेकिन तब ज़्यादा cleanly पढ़ता है जब एक loop किसी template file में सीधे HTML markup के साथ interleaved हो।
उदाहरण: Alternative while Syntax
<?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; ?>
Login to try C/C++/Java/PHP code in the editor
Mathematical Loops
हर iteration पर किसी variable को multiply या halve करना -- fixed amount add या subtract करने के बजाय -- exponential growth या decay produce करता है, जो capacity double करने या किसी search range को बार-बार halve करने जैसी चीज़ों के पीछे का pattern है।
उदाहरण: Mathematical Loops
<?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;
}
?>
Login to try C/C++/Java/PHP code in the editor
- body के अंदर loop variable update करना भूल जाना, एक infinite loop बनाते हुए।
- array counts के साथ
<के बजाय<=इस्तेमाल करना, जो एक iteration ज़्यादा चलाता है और एक undefined index पढ़ता है। - यह मान लेना कि body कम से कम एक बार चलता है, जबकि
whileपहले condition check करता है, इसलिए यह कभी न चले।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: