foreach() while() break 的用法

PHP foreach() while() break 的用法

while (list($key, $value) = each ($arr)) {
echo "Value: $value \n";
}

foreach ($arr as $value) {
echo "Value: $value\n";
}

foreach ($arr as $key => $value) {
echo "Array: $key, $value\n";
}

foreach($_REQUEST as $sF => $value) {
    ${$sF} = $value;
}

================================

範例: (利用 while  break精準取出不同的10個亂數)
 
$gonb=10;
$a=0;
while( !$Array[$a] ){
if( $a>$gonb-1 ) break;
$sn = rand(1,20); 
if(!in_array($sn,$Array)){
$Array[$a]=$sn;
$a++;
}
}
print_r($Array);
Array ( [0] => 17 [1] => 3 [2] => 1 [3] => 13 [4] => 7 [5] => 8 [6] => 12 [7] => 9 [8] => 20 [9] => 6 )

======================
亂出產出資料庫中不同沒有的6位數字,
while(true){
    $client = rand(100000,999999);
    $str = "SELECT client FROM o_user WHERE client=$client";
    $rndQ = mysql_query($str, $plink);
    if(mysql_num_rows($rndQ) == 0) break;
}