note 117186 added to control-structures.do.while
| From: | Martin | Date: | Tue, 28 Apr 2015 15:51:23 +0000 |
| Subject: | note 117186 added to control-structures.do.while | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-202390@lists.php.net to get a copy of this message | ||
Do-while loops can also be used inside other loops, for example:
<?php
// generating an array with random even numbers between 1 and 1000
$numbers = array();
$array_size = 10;
// for loop runs as long as 2nd condition evaluates to true
for ($i=0;$i<$array_size;$i++) {
// always executes (as long as the for-loop runs)
do {
$random = rand(1,1000);
// if the random number is even (condition below is false), the do-while-loop execution ends
// if it's uneven (condition below is true), the loop continues by generating a new random
number
} while (($random % 2) == 1);
// even random number is written to array and for-loop continues iteration until original
condition is met
$numbers[] = $random;
}
// sorting array by alphabet
asort($numbers);
// printing array
echo '<pre>';
print_r($numbers);
echo '</pre>';
?>
----
Server IP: 72.52.91.14
Probable Submitter: 217.26.110.106
----
Manual Page -- http://php.net/manual/en/control-structures.do.while.php
Edit -- https://master.php.net/note/edit/117186
Del: integrated -- https://master.php.net/note/delete/117186/integrated
Del: useless -- https://master.php.net/note/delete/117186/useless
Del: bad code -- https://master.php.net/note/delete/117186/bad+code
Del: spam -- https://master.php.net/note/delete/117186/spam
Del: non-english -- https://master.php.net/note/delete/117186/non-english
Del: in docs -- https://master.php.net/note/delete/117186/in+docs
Del: other reasons-- https://master.php.net/note/delete/117186
Reject -- https://master.php.net/note/reject/117186
Search -- https://master.php.net/manage/user-notes.php