The difference and usage between return and exit, break and contiue in PHP_PHP Tutorial

WBOY
Release: 2016-07-21 15:20:12
Original
839 people have browsed it

Let’s first talk about the usage of exit function.
Function: Output a message and terminate the current script.
If a piece of text includes multiple scripts ending with , exit exits the current script.
For example, if a PHP text includes the following code, the output is world.

<%
echo "hello";
exit;
?>
echo "world";
?>
Syntax format: void representation There is no return value.
void exit ([ string $status ] )
void exit ( int $status )
If status is a string, this function prints status before the script exits.
If status is an integer, this integer will be used as the exit status. Exit status should be from 0 to 254, exit status 255 is reserved by PHP and is prohibited from use. Status 0 is used to indicate successful termination of the procedure.
Usage of return language structure
Role: terminate the execution of a function and return a value from the function
break and continue are used in for, foreach, while, do..while or switch structures.

break ends the execution of the current for, foreach, while, do..while or switch structure.

break can accept an optional numeric parameter to determine how many loops to break out of.

Code:

Copy code The code is as follows:

$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 is used in loop structures Used to skip the remaining code in this loop and start executing the next loop of this loop structure.

Note: Note that in PHP the switch statement is considered a loop structure for continue purposes.

continue accepts an optional numeric parameter to determine how many loops to skip to the end of the loop.

Code:
Copy code The code is as follows:

<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++ < 5) { <BR>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>?>

Note: This article comes from the Internet, the source is unknown

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325167.htmlTechArticleFirst let’s talk about the usage of exit function. Function: Output a message and terminate the current script. If a piece of text includes multiple scripts ending with , exit exits the current script. Than...
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template