PHP while loop Statement:-
while loop का use specify condition के true होने पर block of statement को execute करने एवं repeat करने के लिए किया जाता है
while loop मे एक condition declare की जाती है तथा यदि condition true होती है statement execute होता है यह loop तब तक चलता रहता है जब तक condition false नहीं होती है ।
इसका syntax निम्न है-
SYNTAX:-
while (condition) {
code to be executed;
}
उपरोक्त syntax को समझने के लिए हम निम्न example देखते है ।
Example
<html>
<body>
<?php
$x = 1;
while( $x <= 6) {
echo "The number is: $x <br>" ;
$x++;
}
?>
</body>
</html>
उपरोक्त example मे $x को initialize किया गया है अर्थात $x = 1 declare किया गया है । उसके पश्चात while loop मे एक condition declare की जाती है तथा यदि condition true होती है statement execute होता है
और "++" postfix operator का द्वारा $x variable मे increment किया जाता है । यह loop तब तक चलता रहता है जब तक condition false नहीं होती है
अर्थात ये loop 6 times तक चलता है और 1 to 6 number तक print कर के देता है और 7th time loop चलने पर condition false हो जाता है और loop exit हो जाता है ।
उपरोक्त code को जब Browser पर run करते है तो यह following Output Produce करता है

Figure:- while loop Statement Output