PHP while লুপ
- Previous Page PHP Switch
- Next Page PHP For Loop
PHP while লুপ সুচিন্যতা true হলে কোড ব্লক বাদ্ধান করে
PHP লুপ
আপনি কোড লিখতে যখন, সত্যিই একই কোড ব্লককে বারবার চালাতে হয়। এমন কাজগুলোকে চালানোর জন্য আমরা লুপ ব্যবহার করতে পারি, না তাকেই স্ক্রিপ্টে একইভাবে অনেকগুলো কোড বারবার যোগ করতে।
PHP-তে আমরা এমন কোনো লুপ বিন্যাস আছে:
- while - সুচিন্যতা সত্য হলে কোড ব্লক বাদ্ধান করুন
- do...while - প্রথমে একটি কোড ব্লক বাদ্ধান করুন এবং সুচিন্যতা সত্য হলে ক্রমাগত ব্লক পুনরায় বাদ্ধান করুন
- for - Loop the code block a specified number of times
- foreach - Traverse each element of the array and loop the code block
PHP while লুপ
The while loop will execute the code block as long as the specified condition is true.
Syntax
while (condition is true) { Code to be executed; }
In the following example, the variable $x is first set to 1 ($x=1). Then, the while loop is executed as long as $x is less than or equal to 5. Each time the loop runs, $x is incremented by 1:
Example
<?php $x=1; while($x<=5) { echo "This number is: $x <br>"; $x++; } ?>
PHP do...while Loop
The do...while loop first executes the code block once, then checks the condition, and if the specified condition is true, it repeats the loop.
Syntax
do { Code to be executed; } while (condition is true);
In the following example, the variable $x is first set to 1 ($x=1). Then, the do while loop outputs a string, then increments the variable $x by 1. Later, the condition is checked ($x is less than or equal to 5). As long as $x is less than or equal to 5, the loop will continue to run:
Example
<?php $x=1; do { echo "This number is: $x <br>"; $x++; } while ($x<=5); ?>
Please note that the do while loop checks the condition only after executing the statements inside the loop. This means that the do while loop will execute at least once, even if the condition test fails on the first try.
The following example sets $x to 6, then runs the loop,Check the condition later:
Example
<?php $x=6; do { echo "This number is: $x <br>"; $x++; } while ($x<=5); ?>
The next section will explain the for loop and foreach loop.
- Previous Page PHP Switch
- Next Page PHP For Loop