Home php教程 php手册 PHP中return 和 exit 、break和contiue 区别与用法

PHP中return 和 exit 、break和contiue 区别与用法

Jun 06, 2016 pm 08:40 PM
break return

return、break和contiue是语言结构,就如同if语句之类的,但是exit却是个函数

先说一下exit函数的用法。
作用: 输出一则消息并且终止当前脚本。
如果一段文本中包括多个以 结束的脚本,则exit退出当前所在脚本。
比如一篇php文本包括一下代码,则输出为world。

echo "hello";
exit;
?>
echo "world";
?>
语法格式:void表示没有返回值。
void exit ([ string $status ] )
void exit ( int $status )
如果status是一段字符串,这个函数在脚本退出前打印status。
如果status是一个整数,这个整数会被作为退出状态。退出状态应该从0到254,退出状态255被PHP保留并禁止使用。状态0被用来表示成功的终止程序。
return语言结构的用法
作用:终止函数的执行和从函数中返回一个值
break和continue用在for,foreach,while,do..while 或者 switch 结构中。

break 结束当前 for,foreach,while,do..while 或者 switch 结构的执行。

break 可以接受一个可选的数字参数来决定跳出几重循环。

代码:
代码如下:
$arr = array (‘one', ‘two', ‘three', ‘four', ‘stop', ‘five');
while (list (, $val) = each ($arr)) {
if ($val == ‘stop') {
break;
}
echo "$val
\n";
}

$i = 0;
while (++$i) {
switch ($i) {
case 5:
echo "At 5
\n";
break 1;
case 10:
echo "At 10; quitting
\n";
break 2;
default:
break;
}
}
?>

continue 在循环结构用用来跳过本次循环中剩余的代码并开始执行本循环结构的下一次循环。

注: 注意在 PHP 中 switch 语句被认为是作为 continue 目的的循环结构。

continue 接受一个可选的数字参数来决定跳过几重循环到循环结尾。

代码:
代码如下:
<br><?php <BR>while (list ($key, $value) = each ($arr)) { <br>if (!($key % 2)) { // skip odd members <br>continue; <br>} <br>do_something_odd ($value); <br>} <br>$i = 0; <br>while ($i++ echo "Outer<br>\n"; <br>while (1) { <br>echo " Middle<br>\n"; <br>while (1) { <br>echo " Inner<br>\n"; <br>continue 3; <br>} <br>echo "This never gets output.<br>\n"; <br>} <br>echo "Neither does this.<br>\n"; <br>} <br>?>

注明:本段文章来自互联网,出处不详
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed explanation of the usage of return in C language Detailed explanation of the usage of return in C language Oct 07, 2023 am 10:58 AM

The usage of return in C language is: 1. For functions whose return value type is void, you can use the return statement to end the execution of the function early; 2. For functions whose return value type is not void, the function of the return statement is to end the execution of the function. The result is returned to the caller; 3. End the execution of the function early. Inside the function, we can use the return statement to end the execution of the function early, even if the function does not return a value.

What is the execution order of return and finally statements in Java? What is the execution order of return and finally statements in Java? Apr 25, 2023 pm 07:55 PM

Source code: publicclassReturnFinallyDemo{publicstaticvoidmain(String[]args){System.out.println(case1());}publicstaticintcase1(){intx;try{x=1;returnx;}finally{x=3;}}}#Output The output of the above code can simply conclude: return is executed before finally. Let's take a look at what happens at the bytecode level. The following intercepts part of the bytecode of the case1 method, and compares the source code to annotate the meaning of each instruction in

How does Vue3 use setup syntax sugar to refuse to write return How does Vue3 use setup syntax sugar to refuse to write return May 12, 2023 pm 06:34 PM

Vue3.2 setup syntax sugar is a compile-time syntax sugar that uses the combined API in a single file component (SFC) to solve the cumbersome setup in Vue3.0. The declared variables, functions, and content introduced by import are exposed through return, so that they can be used in Vue3.0. Problems in use 1. There is no need to return declared variables, functions and content introduced by import during use. You can use syntactic sugar //import the content introduced import{getToday}from'./utils'//variable constmsg='Hello !'//function func

Detailed explanation of JavaScript function return values ​​and return statements Detailed explanation of JavaScript function return values ​​and return statements Aug 04, 2022 am 09:46 AM

JavaScript functions provide two interfaces to interact with the outside world. The parameters serve as the entrance to receive external information; the return value serves as the outlet to feed back the operation results to the outside world. The following article will take you to understand the JavaScript function return value and briefly analyze the usage of the return statement. I hope it will be helpful to you!

Use the return keyword in JavaScript Use the return keyword in JavaScript Feb 18, 2024 pm 12:45 PM

Usage of return in JavaScript requires specific code examples In JavaScript, the return statement is used to specify the value returned from a function. Not only can it be used to end the execution of a function, it can also return a value to the place where the function was called. The return statement has the following common uses: Return a value The return statement can be used to return a value to the place where the function is called. Here is a simple example: functionadd(a,b){

How to use return value in Python How to use return value in Python Oct 07, 2023 am 11:10 AM

Python return value return usage is that when the function executes the return statement, execution will stop immediately and the specified value will be returned to the place where the function was called. Detailed usage: 1. Return a single value; 2. Return multiple values; 3. Return a null value; 4. End the execution of the function early.

Let's talk about not using break in PHP switch statement Let's talk about not using break in PHP switch statement Mar 20, 2023 pm 04:55 PM

It is very common to use switch statements to select multiple branches in PHP. Usually, a break statement is used to exit the switch statement after each branch. However, there are situations where we do not want to use the break statement. This article will introduce the situation of not using break in the PHP switch statement.

What is the usage of break in php What is the usage of break in php Jan 31, 2023 pm 07:33 PM

In PHP, break is used to jump out of the current syntax structure and execute the following statements; it can be used in statements such as switch, for, while, and do while. It can terminate the code of the loop body and jump out of the current loop immediately, and execute the following statements after the loop. code. The break statement can take a parameter n, which represents the number of levels to jump out of the loop. If you want to jump out of multiple loops, you can use n to represent the number of levels to jump out of. If there is no parameter, the default is to jump out of the current loop.

See all articles