您当前的位置: 首页 > 网站编程 > PHP教程 > php 中 return exit break contiue 详解

php 中 return exit break contiue 详解

作者:不详 来源:网络 发布时间: 2013-10-10 10:12 点击:
return、break和contiue是语言结构,就如同if语句之类的,但是exit却是个函数。 1.exit函数 作用:Output a message and terminate the current script 输出一则消息并且终止当前脚本。 如果一段文本中包括多个以 ?php 开始,以 ? 结束的脚本,exit退出所有脚本。 比如

php 中 return exit break contiue 详解

    return、break和contiue是语言结构,就如同if语句之类的,但是exit却是个函数。
  
  1.exit函数
  
  作用:Output a message and terminate the current script
  
  输出一则消息并且终止当前脚本。
  
  如果一段文本中包括多个以 <?php 开始,以 ?> 结束的脚本,exit退出所有脚本。
  
  比如一篇php文本包括一下代码,则不输出为world。
  
  <?php
  
  echo "hello";
  
  exit;
  
  ?>
  
  <?php
  
  echo "world";
  
  ?>
  
  语法格式: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被用来表示成功的终止程序。
  
  2.return语言结构的用法
  
  作用:终止函数的执行和从函数中返回一个值
  
  3.break和continue
  
  用在for,foreach,while,do..while 或者 switch 结构中。
  
  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;
  
  }
  
  echo "$val<br>\n";
  
  }
  
  $i = 0;
  
  while (++$i) {
  
  switch ($i) {
  
  case 5:
  
  echo "At 5<br>\n";
  
  break 1;
  
  case 10:
  
  echo "At 10; quitting<br>\n";
  
  break 2;
  
  default:
  
  break;
  
  }
  
  }
  
  ?>
  
  continue 在循环结构用用来跳过本次循环中剩余的代码并开始执行本循环结构的下一次循环。
  
  注: 注意在 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>\n";
  
  while (1) {
  
  echo " Middle<br>\n";
  
  while (1) {
  
  echo " Inner<br>\n";
  
  continue 3;
  
  }
  
  echo "This never gets output.<br>\n";
  
  }
  
  echo "Neither does this.<br>\n";
  
  }
  
  ?>
分享到:
本文"php 中 return exit break contiue 详解"由远航站长收集整理而来,仅供大家学习与参考使用。更多网站制作教程尽在远航站长站。
顶一下
(1)
100%
踩一下
(0)
0%
[点击 次] [返回上一页] [打印]
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 密码: 验证码:
关于本站 - 联系我们 - 网站声明 - 友情连接- 网站地图 - 站点地图 - 返回顶部
Copyright © 2007-2013 www.yhzhan.com(远航站长). All Rights Reserved .
远航站长:为中小站长提供最佳的学习与交流平台,提供网页制作与网站编程等各类网站制作教程.
官方QQ:445490277 网站群:26680406 网站备案号:豫ICP备07500620号-4