Valkyrie Destruction The difference and usage of return and exit, break, and Contiue in PHP

Source: Internet
Author: User
Tags terminates
Let's talk about the use of the Exit function.
Function: Outputs a message and terminates the current script.
If a text contains multiple scripts to end, exit exits the current script.
For example, a PHP text includes code, then the output is world.
<%
echo "Hello";
Exit
?>
echo "World";
?>
Syntax format: Void indicates no return value.
void exit ([string $status])
void exit (int $status)
If status is a string, this function prints the status before the script exits.
If the status is an integer, the integer will be used as the exit state. Exit status should be from 0 to 254, exit status 255 is reserved by PHP and is forbidden to use. Status 0 is used to indicate a successful termination program.
The use of the return language structure
Function: Terminates the execution of the function and returns a value from the function
Break and continue used in For,foreach,while,do. The while or switch structure.
Break ends Current For,foreach,while,do: The execution of the while or switch structure.
Break can accept an optional numeric parameter to decide to jump out of a few loops.
Code:

Copy the Code code as follows:


$arr = Array (' One ', ' one ', ' one ', ' 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
}
}
?>


The Continue is used in the loop structure to skip the remaining code in this loop and begin the next loop of the loop structure.
Note: Notice that the switch statement in PHP is considered a looping structure for continue purposes.
Continue accepts an optional numeric parameter to decide to skip a few loops to the end of the loop.
Code:

Copy the Code code as follows:



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


Note: This paragraph of the article from the Internet, the source is unknown

The above describes the Valkyrie destruction PHP return and exit, break and contiue differences and usage, including the Valkyrie destruction aspects, I hope to be interested in PHP tutorial friends helpful.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.