PHP for Loop
In this page:
for ($i = start; $i <= end; $i++) {
// body
}
एक for Loop क्या है?
एक for loop सही tool है जब आपको पहले से पता हो कि आपको कुछ कितनी बार दोहराना है।
इसका header एक line में तीन parts pack करता है — counter कहाँ से शुरू होता है, वह condition जो loop को चलाते रखती है, और हर pass के बाद counter कैसे बदलता है।
उदाहरण: What is a for Loop?
<?php
// Classic for-loop: `$i = 1; $i <= 5; $i++`
for ($i = 1; $i <= 5; $i++) {
// Print `$i . "\n"` to the output
echo $i . "\n";
}
?>
Login to try C/C++/Java/PHP code in the editor
Custom Loop Steps
किसी for loop के header में step (increment) expression सिर्फ 1 add करने पर locked नहीं है — आप हर दूसरी value skip करने के लिए 2 add कर सकते हैं, exponential steps के लिए multiply कर सकते हैं, या कोई भी expression इस्तेमाल कर सकते हैं, जब तक यह आख़िरकार loop की condition को false बना दे।
उदाहरण: Custom Loop Steps
<?php
// Classic for-loop: `$i = 0; $i <= 10; $i += 2`
for ($i = 0; $i <= 10; $i += 2) {
// Print `$i . "\n"` to the output
echo $i . "\n";
}
?>
Login to try C/C++/Java/PHP code in the editor
Indexed Arrays में Loop चलाना
loop counter को सीधे एक array index ($array[$i]) की तरह इस्तेमाल करना exactly वजह है कि for loops arrays के साथ इतनी naturally pair होते हैं — आपको हर single iteration में current position और corresponding value दोनों मिलती हैं, बिना किसी extra bookkeeping के।
उदाहरण: Looping Through Indexed Arrays
<?php
// Declare `$fruits` as an array: `["apple", "banana", "cherry"]`
$fruits = ["apple", "banana", "cherry"];
// Classic for-loop: `$i = 0; $i < count($fruits); $i++`
for ($i = 0; $i < count($fruits); $i++) {
// Print `$i . ": " . $fruits[$i] . "\n"` to the output
echo $i . ": " . $fruits[$i] . "\n";
}
?>
Login to try C/C++/Java/PHP code in the editor
Nested for Loops
एक for loop दूसरे के अंदर nested outer वाले के हर single iteration के लिए inner loop को पूरी तरह complete चलाता है — grid-shaped किसी भी चीज़ के लिए classic pattern, जैसे एक multiplication table print करना या किसी 2D array की rows और columns में चलना।
उदाहरण: Nested for Loops
<?php
// Classic for-loop: `$i = 1; $i <= 2; $i++`
for ($i = 1; $i <= 2; $i++) {
// Classic for-loop: `$j = 1; $j <= 2; $j++`
for ($j = 1; $j <= 2; $j++) {
// Print "$i x $j = " . ($i * $j) . "\n" to the output
echo "$i x $j = " . ($i * $j) . "\n";
}
}
?>
Login to try C/C++/Java/PHP code in the editor
Alternative Syntax
PHP का alternative syntax opening { को एक colon से और closing } को endfor; से swap करता है, जो — exactly if और while की तरह — किसी template में जब इसकी body HTML output के साथ interleaved हो तो loop को कहीं ज़्यादा readable रखता है।
उदाहरण: Alternative Syntax
<?php // Repeat 3 times, printing a numbered row each time ?>
<?php for ($i = 1; $i <= 3; $i++): ?>
<p>Row <?php echo $i; ?></p>
<?php endfor; ?>
Login to try C/C++/Java/PHP code in the editor
count($arr)के साथ<=इस्तेमाल करना, ताकि last pass$arr[count($arr)]पढ़े जो undefined है।- एक बड़े loop की condition में
count($arr)call करना जब इसे loop से पहले एक बार store किया जा सकता था। for (...)header के बाद एक semicolon लगाना, जो loop body को खाली बना देता है और अगले block को एक बार चलाता है।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: