標籤:style blog color 使用 io 檔案 for ar
1.break跳出程式碼片段,結束這個迴圈
2.continue結束當前片段,結束這次迴圈,繼續下一次迴圈
3.exit 結束整個PHP代碼
break的作用是跳出這個迴圈(如果這個break或者continue在迴圈中的if語句裡面,不是跳出if語句,而是跳出迴圈語句),執行這個迴圈的大括弧以後的語句,
break在迴圈語句中是這樣,在switch語句用功能也是這樣的,而continue是當條件滿足的時候不執行這個迴圈後面的語句,從這個迴圈的開始重新執行。
1 <?php 2 /* php的break,continue,return 的簡單區別代碼 */ 3 $i = 1; 4 while (true) { // 這裡看上去這個迴圈會一直執行 5 if ($i==2) {// 2跳過不顯示 6 $i++; 7 continue; 8 } else if ($i==5) {// 但到這裡$i=5就跳出循迴圈了 9 break; 10 } else { 11 echo $i . ‘<br>‘; 12 } 13 $i++; 14 } 15 exit; 16 17 echo ‘這裡不輸出‘;18 ?>
return、break和contiue是語言結構,就如同if語句之類的,但是exit卻是個函數。
先說一下exit函數的用法:
作用:Output a message and terminate the current script 輸出一則訊息並且終止當前指令碼。
如果一段文本中包括多個以 <?php 開始,以 ?> 結束的指令碼,exit退出所有指令碼。
比如一篇php文本包括一下代碼,則不輸出為world。
1 <?php 3 echo "hello"; 5 exit; 7 ?> 8 9 <?php11 echo "world";13 ?>
文法格式:void表示沒有傳回值。
void exit ([ string $status ] )
void exit ( int $status )
If status is a string, this function prints the status just before exiting.
如果status是一段字串,這個函數在指令碼退出前列印status。
If status is an integer, that value will also be used as the exit status. Exit statuses should be in the range 0 to 254, the exit status 255 is reserved by PHP and shall not be used. The status 0 is used to terminate the program successfully.
如果status是一個整數,這個整數會被作為退出狀態。退出狀態應該從0到254,退出狀態255被PHP保留並禁止使用。狀態0被用來表示成功的終止程式。
break
break 結束當前 for,foreach,while,do-while 或者 switch 結構的執行。
break 可以接受一個可選的數字參數來決定跳出幾重迴圈。
<?php
$arr = array(‘one‘, ‘two‘, ‘three‘, ‘four‘, ‘stop‘, ‘five‘);
while (list (, $val) = each($arr)) {
if ($val == ‘stop‘) {
break; /* You could also write ‘break 1;‘ here. */
}
echo "$val<br /> ";
}
/* 使用選擇性參數 */
$i = 0;
while (++$i) {
switch ($i) {
case 5:
echo "At 5<br /> ";
break 1; /* 只退出 switch. */
case 10:
echo "At 10; quitting<br /> ";
break 2; /* 退出 switch 和 while 迴圈 */
default:
break;
}
}
?>
continue
continue 在迴圈結構用用來跳過本次迴圈中剩餘的代碼並在條件求值為真時開始執行下一次迴圈。
Note: 注意在 PHP 中 switch 語句被認為是可以使用 continue 的一種迴圈結構。
continue 接受一個可選的數字參數來決定跳過幾重迴圈到迴圈結尾。
<?php
while (list ($key, $value) = each($arr)) {
if (!($key % 2)) { // skip odd members
continue;
}
do_something_odd($value);
}
$i = 0;
while ($i++ < 5) {
echo "Outer<br /> ";
while (1) {
echo " Middle<br /> ";
while (1) {
echo " Inner<br /> ";
continue 3;
}
echo "This never gets output.<br /> ";
}
echo "Neither does this.<br /> ";
}
?>
省略 continue 後面的分號會導致混淆。以下例子示意了不應該這樣做。
<?php
for ($i = 0; $i < 5; ++$i) {
if ($i == 2)
continue
print "$i ";
}
?>
希望得到的結果是:
0134
可實際的輸出是:
2
因為 print() 調用的傳回值是 int(1),看上去作為了上述可選的數字參數。
return
如果在一個函數中調用 return() 語句,將立即結束此函數的執行並將它的參數作為函數的值返回。return() 也會終止 eval() 語句或者指令檔的執行。
如果在全域範圍中調用,則當前指令檔中止運行。如果當前指令檔是被 include() 的或者require() 的,則控制交回調用檔案。此外,如果當前指令碼是被 include() 的,則 return()的值會被當作 include() 調用的傳回值。如果在主指令檔中調用 return(),則指令碼中止運行。如果當前指令檔是在 php.ini 中的配置選項 auto_prepend_file 或者 auto_append_file 所指定的,則此指令檔中止運行。
更多資訊見傳回值。
Note: 注意既然 return() 是語言結構而不是函數,因此其參數沒有必要用括弧將其括起來。通常都不用括弧,實際上也應該不用,這樣可以降低 PHP 的負擔。
Note: 當用引用傳回值時永遠不要使用括弧,這樣行不通。只能通過引用返回變數,而不是語句的結果。如果使用 return ($a); 時其實不是返回一個變數,而是運算式 ($a) 的值(當然,此時該值也正是 $a 的值)。