PHP while Loop In Hindi

📔 : PHP 🔗

Actually किसी code of block को repeatedly run करने का सबसे easy way looping है , PHP में different - different Looping Statements हैं -

इस Topic में हम while Loop के बारे में पढ़ेंगे


PHP में  While Loop ठीक वैसे ही काम करते हैं जिस तरह से  या  JAVA  में करते हैं। While Loop  Simply Nested Statements को Execute करता है , जब तक कि दी हुई Condition False न हो। जब तक Condition True है तब तक Loop Execute होगा।    While Loop  को Entry Control Loop भी कहते हैं क्योंकि loop को Execute करने से पहले दी हुई condition Check  होती है  , condition True होने पर ही Loop में Entry होती है।

PHP while Loop Syntax

while(condition / expression)
{
   //write your logic
}

PHP While Loop Chart Flow

PHP While Loop Chart Flow

PHP While Loop Example

File : while.php

Copy Fullscreen Close Fullscreen
<?php 
   /*Example 1 to print print 1 to 10*/
   $x  = 1;
   while ($x <= 10) 
   {
      echo $x.", ";
      /*increment by 1*/
      ++$x;
   } 
   
   /*break the line*/
   echo "<br>";
   /*Example 2 to print print 1 to 10*/
   $x  = 1;
   while ($x <= 10) :
      echo $x.", ";
      /*increment by 1*/
      ++$x;
   endwhile;      
?>
Output
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
1, 2, 3, 4, 5, 6, 7, 8, 9, 10

चूंकि मैं पहले भी बता चुका हूँ कि PHP में हम colon (:) का use भी कर सकते हैं इसलिए ऊपर दिए गए example में दो बार 1 से 10 तक print हुआ।

PHP Nested While Loop

 इसके अलावा आप PHP में Nested While Loop का भी use कर सकते हैं , means While Loop के अंदर एक और While Loop.

Example -

File : nested_while.php

Copy Fullscreen Close Fullscreen
<?php 
 $x = 1;
 while($x <= 10)
 {
   $y = 1;
   echo $y." ";
   while($y < $x)
   {
     $y++;
     echo $y." ";
   }
   
   ++$x;
   echo"<br>";
 }      
?>
Output
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10

I hope अब आप While Loop और Nested While Loop के बारे में अच्छी तरह से समझ गए होंगे।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! My name is Rahul Kumar Rajput. I'm a back end web developer and founder of learnhindituts.com. I live in Uttar Pradesh (UP), India and I love to talk about programming as well as writing technical tutorials and tips that can help to others.

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers