Home > Backend Development > PHP Tutorial > Differences in usage of the four structures return, exit, break and continue in PHP

Differences in usage of the four structures return, exit, break and continue in PHP

伊谢尔伦
Release: 2023-03-11 09:16:01
Original
1293 people have browsed it

return, break and contiue are language structures, just like if statements, but exit is a function

Let’s talk about the usage of the exit function first.
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 will be world.

<php 
echo "hello"; 
exit; 
?> 
echo "world"; 
?>
Copy after login

Syntax format: void means no return value.
void exit ([ string $status ] )
void exit ( int $status )
If status is a string, this function will be executed before the script exits Print status.
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
Function: terminate the execution of the function and return a value from the function
break and continue are used in for, foreach, while, do..while or switch structure.
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:

<?php
$arr = array (‘one&#39;, ‘two&#39;, ‘three&#39;, ‘four&#39;, ‘stop&#39;, ‘five&#39;); 
while (list (, $val) = each ($arr)) { 
if ($val == ‘stop&#39;) { 
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; 
} 
} 
?>
Copy after login

continue is used in the loop structure 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:

<code> 
<?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"; 
} 
?></code>
Copy after login

The above is the detailed content of Differences in usage of the four structures return, exit, break and continue in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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