php 中 return exit break contiue 詳解

發(fā)布時(shí)間:2013-9-25 15:52    發(fā)布者:reggae
關(guān)鍵詞: php
return、break和contiue是語(yǔ)言結構,就如同if語(yǔ)句之類(lèi)的,但是exit卻是個(gè)函數。

1.exit函數
作用:Output a message and terminate the current script
輸出一則消息并且終止當前腳本。
如果一段文本中包括多個(gè)以 結束的腳本,exit退出所有腳本。
比如一篇php文本包括一下代碼,則不輸出為world。
  1. echo "hello";
  2. exit;
  3. ?>
  4. echo "world";
  5. ?>
復制代碼

語(yǔ)法格式:void表示沒(méi)有返回值。
void exit ([ string $status ] )
void exit ( int $status )
If status is a string, this function prints the status just before exiting.
如果status是一段字符串,這個(gè)函數在腳本退出前打印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是一個(gè)整數,這個(gè)整數會(huì )被作為退出狀態(tài)。退出狀態(tài)應該從0到254,退出狀態(tài)255被PHP保留并禁止使用。狀態(tài)0被用來(lái)表示成功的終止程序。

2.return語(yǔ)言結構的用法

作用:終止函數的執行和從函數中返回一個(gè)值

3.break和continue

用在for,foreach,while,do..while 或者 switch 結構中。

break 結束當前 for,foreach,while,do..while 或者 switch 結構的執行。

break 可以接受一個(gè)可選的數字參數來(lái)決定跳出幾重循環(huán)。

代碼:

  1. $arr = array ('one', 'two', 'three', 'four', 'stop', 'five');
  2. while (list (, $val) = each ($arr)) {
  3. if ($val == 'stop') {
  4. break;
  5. }
  6. echo "$val
    \n";
  7. }


  8. $i = 0;
  9. while (++$i) {
  10. switch ($i) {
  11. case 5:
  12. echo "At 5
    \n";
  13. break 1;
  14. case 10:
  15. echo "At 10; quitting
    \n";
  16. break 2;
  17. default:
  18. break;
  19. }
  20. }
  21. ?>
復制代碼

continue 在循環(huán)結構用用來(lái)跳過(guò)本次循環(huán)中剩余的代碼并開(kāi)始執行本循環(huán)結構的下一次循環(huán)。

注: 注意在 PHP 中 switch 語(yǔ)句被認為是作為 continue 目的的循環(huán)結構。

continue 接受一個(gè)可選的數字參數來(lái)決定跳過(guò)幾重循環(huán)到循環(huán)結尾。

代碼:

  1. while (list ($key, $value) = each ($arr)) {
  2. if (!($key % 2)) { // skip odd members
  3. continue;
  4. }
  5. do_something_odd ($value);
  6. }

  7. $i = 0;
  8. while ($i++ < 5) {
  9. echo "Outer
    \n";
  10. while (1) {
  11. echo " Middle
    \n";
  12. while (1) {
  13. echo " Inner
    \n";
  14. continue 3;
  15. }
  16. echo "This never gets output.
    \n";
  17. }
  18. echo "Neither does this.
    \n";
  19. }
  20. ?>
復制代碼
以上是本文關(guān)于php 中 return exit break contiue 的詳解,希望本文對廣大php開(kāi)發(fā)者有所幫助,感謝閱讀本文。更多php技術(shù)問(wèn)題歡迎加群探討:256271784,驗證碼:eec,不寫(xiě)驗證不予通過(guò)喲~

本文地址:http://selenalain.com/thread-121347-1-1.html     【打印本頁(yè)】

本站部分文章為轉載或網(wǎng)友發(fā)布,目的在于傳遞和分享信息,并不代表本網(wǎng)贊同其觀(guān)點(diǎn)和對其真實(shí)性負責;文章版權歸原作者及原出處所有,如涉及作品內容、版權和其它問(wèn)題,我們將根據著(zhù)作權人的要求,第一時(shí)間更正或刪除。
您需要登錄后才可以發(fā)表評論 登錄 | 立即注冊

關(guān)于我們  -  服務(wù)條款  -  使用指南  -  站點(diǎn)地圖  -  友情鏈接  -  聯(lián)系我們
電子工程網(wǎng) © 版權所有   京ICP備16069177號 | 京公網(wǎng)安備11010502021702
快速回復 返回頂部 返回列表
午夜高清国产拍精品福利|亚洲色精品88色婷婷七月丁香|91久久精品无码一区|99久久国语露脸精品|动漫卡通亚洲综合专区48页