PHP/Statement/do while
Содержание
Break in Nested Loops
<html>
<head>
<title>Break Nested</title>
</head>
<body>
<?php
srand((double)microtime() * 1000000);
$number = rand(1, 1000);
$j = 0;
$outer_loop_itr = 50;
for($i = 1; $i <= $outer_loop_itr; $i++) {
$j = 0;
do {
$j++;
$number = rand(1, 1000);
if($number == 999) {
break;
}
} while(1);
$num_iterations[$i] = $j;
}
$avg_iterations = (array_sum($num_iterations) / $outer_loop_itr);
print("an average of " . $avg_iterations);
?>
</body>
</html>
Counting to 10 with do ... while
<?php
$num = 1;
do {
echo "Number is ".$num."<br />";
$num++;
} while ($num <= 10);
echo "Done.";
?>
Do while loop
<?php
$a = 1;
do{
echo $a . "<br>";
$a++;
}while ($a > 10);
?>
do...while loop is executed at least once
<?php
$i = 11;
do {
print "Number $i\n";
} while ($i < 10);
?>
Same code could be written using a while loop:
<?php
$i = 11;
while ($i < 10) {
print "Number $i\n";
}
?>
do while loop with counter
<?php
$num = 11;
do {
echo $num;
$num++;
} while ($num <= 10);
?>
do while with integer counter
<?php
$count = 11;
do {
echo "$count squared = ".pow($count,2). "<br />";
} while ($count < 10);
?>