PHP鎺у埗缁撴瀯涔媐or
for 寰幆鏄 PHP 涓渶澶嶆潅鐨勫惊鐜粨鏋勩傚畠鐨勮涓哄拰 C 璇█鐨勭浉浼笺
for
for 寰幆鏄 PHP 涓渶澶嶆潅鐨勫惊鐜粨鏋勩傚畠鐨勮涓哄拰 C 璇█鐨勭浉浼笺 for 寰幆鐨勮娉曟槸锛
for (expr1; expr2; expr3)
statement
绗竴涓〃杈惧紡锛坋xpr1锛夊湪寰幆寮濮嬪墠鏃犳潯浠舵眰鍊间竴娆°
expr2 鍦ㄦ瘡娆″惊鐜紑濮嬪墠姹傚笺傚鏋滃间负 TRUE锛屽垯缁х画寰幆锛屾墽琛屽祵濂楃殑寰幆璇彞銆傚鏋滃间负 FALSE锛屽垯缁堟寰幆銆
expr3 鍦ㄦ瘡娆″惊鐜箣鍚庤姹傚硷紙鎵ц锛夈
姣忎釜琛ㄨ揪寮忛兘鍙互涓虹┖鎴栧寘鎷楀彿鍒嗛殧鐨勫涓〃杈惧紡銆傝〃杈惧紡 expr2 涓紝鎵鏈夌敤閫楀彿鍒嗛殧鐨勮〃杈惧紡閮戒細璁$畻锛屼絾鍙彇鏈鍚庝竴涓粨鏋溿俥xpr2 涓虹┖鎰忓懗鐫灏嗘棤闄愬惊鐜笅鍘伙紙鍜 C 涓鏍凤紝PHP 璁や负鍏跺间负 TRUE锛夈傝繖鍙兘涓嶅儚鎯宠薄涓偅鏍锋病鏈夌敤锛屽洜涓虹粡甯镐細甯屾湜鐢 break 璇彞鏉ョ粨鏉熷惊鐜屼笉鏄敤 for 鐨勮〃杈惧紡鐪熷煎垽鏂
鑰冭檻浠ヤ笅鐨勪緥瀛愶紝瀹冧滑閮芥樉绀烘暟瀛 1 鍒 10锛 <?php
/* example 1 */
for ($i = 1; $i <= 10; $i++) {
echo $i;
}
/* example 2 */
for ($i = 1; ; $i++) {
if ($i > 10) {
break;
}
echo $i;
}
/* example 3 */
$i = 1;
for (;;) {
if ($i > 10) {
break;
}
echo $i;
$i++;
}
/* example 4 */
for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++);
?>
褰撶劧锛岀涓涓緥瀛愮湅涓婂幓鏈姝e父锛堟垨鑰呯鍥涗釜锛夛紝浣嗙敤鎴峰彲鑳戒細鍙戠幇鍦 for 寰幆涓敤绌虹殑琛ㄨ揪寮忓湪寰堝鍦哄悎涓嬩細寰堟柟渚裤
PHP 涔熸敮鎸佺敤鍐掑彿鐨 for 寰幆鐨勬浛浠h娉曘
for (expr1; expr2; expr3):
statement;
...
endfor;
鎴戜滑缁忓父闇瑕佸涓嬮潰杩欐牱鐨勬暟缁勮繘琛岄亶鍘嗭細
<?php
/*
* 鎴戜滑鎯宠鍦ㄩ亶鍘嗙殑杩囩▼涓敼鍙樹互涓嬫暟缁勪腑鏌愪簺鍏冪礌鐨勫
*/
$people = Array(
Array('name' => 'Kalle', 'salt' => 856412),
Array('name' => 'Pierre', 'salt' => 215863)
);
for($i = 0; $i < sizeof($people); ++$i)
{
$people[$i]['salt'] = rand(000000, 999999);
}
?>
浠ヤ笂浠g爜鐨勯棶棰橈紝鍦ㄤ簬for鐨勭浜屼釜琛ㄨ揪寮忎細瀵艰嚧浠g爜鎵ц寰堟參锛嶏紞鍥犱负姣忔寰幆鏃堕兘瑕 璁$畻涓閬嶆暟缁勭殑闀垮害銆傜敱浜庢暟缁勭殑闀垮害濮嬬粓涓嶅彉锛屾垜浠彲浠ョ敤涓涓腑闂村彉閲忔潵瀛樺偍鏁扮粍闀垮害锛 鐒跺悗鐢ㄨ繖涓彉閲忎綔涓篺or寰幆鐨勭浜屼釜琛ㄨ揪寮忋傝繖鏍峰湪寰幆鐨勬椂鍊欏氨鍙互鐩存帴浣跨敤璇ュ彉閲忕殑鍊硷紝 涓嶇敤姣忔閲嶆柊璁$畻銆 濡備笅锛
<?php
$people = Array(
Array('name' => 'Kalle', 'salt' => 856412),
Array('name' => 'Pierre', 'salt' => 215863)
);
for($i = 0, $size = sizeof($people); $i < $size; ++$i)
{
$people[$i]['salt'] = rand(000000, 999999);
}
?>
for 寰幆鏄 PHP 涓渶澶嶆潅鐨勫惊鐜粨鏋勩傚畠鐨勮涓哄拰 C 璇█鐨勭浉浼笺 for 寰幆鐨勮娉曟槸锛
for (expr1; expr2; expr3)
statement
绗竴涓〃杈惧紡锛坋xpr1锛夊湪寰幆寮濮嬪墠鏃犳潯浠舵眰鍊间竴娆°
expr2 鍦ㄦ瘡娆″惊鐜紑濮嬪墠姹傚笺傚鏋滃间负 TRUE锛屽垯缁х画寰幆锛屾墽琛屽祵濂楃殑寰幆璇彞銆傚鏋滃间负 FALSE锛屽垯缁堟寰幆銆
expr3 鍦ㄦ瘡娆″惊鐜箣鍚庤姹傚硷紙鎵ц锛夈
姣忎釜琛ㄨ揪寮忛兘鍙互涓虹┖鎴栧寘鎷楀彿鍒嗛殧鐨勫涓〃杈惧紡銆傝〃杈惧紡 expr2 涓紝鎵鏈夌敤閫楀彿鍒嗛殧鐨勮〃杈惧紡閮戒細璁$畻锛屼絾鍙彇鏈鍚庝竴涓粨鏋溿俥xpr2 涓虹┖鎰忓懗鐫灏嗘棤闄愬惊鐜笅鍘伙紙鍜 C 涓鏍凤紝PHP 璁や负鍏跺间负 TRUE锛夈傝繖鍙兘涓嶅儚鎯宠薄涓偅鏍锋病鏈夌敤锛屽洜涓虹粡甯镐細甯屾湜鐢 break 璇彞鏉ョ粨鏉熷惊鐜屼笉鏄敤 for 鐨勮〃杈惧紡鐪熷煎垽鏂
鑰冭檻浠ヤ笅鐨勪緥瀛愶紝瀹冧滑閮芥樉绀烘暟瀛 1 鍒 10锛 <?php
/* example 1 */
for ($i = 1; $i <= 10; $i++) {
echo $i;
}
/* example 2 */
for ($i = 1; ; $i++) {
if ($i > 10) {
break;
}
echo $i;
}
/* example 3 */
$i = 1;
for (;;) {
if ($i > 10) {
break;
}
echo $i;
$i++;
}
/* example 4 */
for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++);
?>
褰撶劧锛岀涓涓緥瀛愮湅涓婂幓鏈姝e父锛堟垨鑰呯鍥涗釜锛夛紝浣嗙敤鎴峰彲鑳戒細鍙戠幇鍦 for 寰幆涓敤绌虹殑琛ㄨ揪寮忓湪寰堝鍦哄悎涓嬩細寰堟柟渚裤
PHP 涔熸敮鎸佺敤鍐掑彿鐨 for 寰幆鐨勬浛浠h娉曘
for (expr1; expr2; expr3):
statement;
...
endfor;
鎴戜滑缁忓父闇瑕佸涓嬮潰杩欐牱鐨勬暟缁勮繘琛岄亶鍘嗭細
<?php
/*
* 鎴戜滑鎯宠鍦ㄩ亶鍘嗙殑杩囩▼涓敼鍙樹互涓嬫暟缁勪腑鏌愪簺鍏冪礌鐨勫
*/
$people = Array(
Array('name' => 'Kalle', 'salt' => 856412),
Array('name' => 'Pierre', 'salt' => 215863)
);
for($i = 0; $i < sizeof($people); ++$i)
{
$people[$i]['salt'] = rand(000000, 999999);
}
?>
浠ヤ笂浠g爜鐨勯棶棰橈紝鍦ㄤ簬for鐨勭浜屼釜琛ㄨ揪寮忎細瀵艰嚧浠g爜鎵ц寰堟參锛嶏紞鍥犱负姣忔寰幆鏃堕兘瑕 璁$畻涓閬嶆暟缁勭殑闀垮害銆傜敱浜庢暟缁勭殑闀垮害濮嬬粓涓嶅彉锛屾垜浠彲浠ョ敤涓涓腑闂村彉閲忔潵瀛樺偍鏁扮粍闀垮害锛 鐒跺悗鐢ㄨ繖涓彉閲忎綔涓篺or寰幆鐨勭浜屼釜琛ㄨ揪寮忋傝繖鏍峰湪寰幆鐨勬椂鍊欏氨鍙互鐩存帴浣跨敤璇ュ彉閲忕殑鍊硷紝 涓嶇敤姣忔閲嶆柊璁$畻銆 濡備笅锛
<?php
$people = Array(
Array('name' => 'Kalle', 'salt' => 856412),
Array('name' => 'Pierre', 'salt' => 215863)
);
for($i = 0, $size = sizeof($people); $i < $size; ++$i)
{
$people[$i]['salt'] = rand(000000, 999999);
}
?>
浠ヤ笂灏辨槸PHP鎺у埗缁撴瀯涔媐or鐨勮缁嗗唴瀹癸紝鏇村淇℃伅璇峰叧娉∣D浜戝叾瀹冪浉鍏虫枃绔狅紒