PHP return and exit, break and Contiue differences and usage _php tips

Source: Internet
Author: User
Let's talk about the use of the Exit function.
Function: output a message and terminate the current script.
If a paragraph of text contains more than one script that ends, exit exits the current script.
For example, a PHP text including the code, then output to the world.

<%
echo "Hello";
Exit
?>
echo "World";
?>
Syntax format: void means 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 the status is an integer, the integer is used as an exit state. Exit status should be from 0 to 254, exit State 255 is reserved by PHP and is not allowed. State 0 is used to indicate a successful termination program.
Use of the return language structure
Function: Terminate the execution of a function and return a value from a function
Break and continue used in For,foreach,while,do. While or in the switch structure.

Break End Current For,foreach,while,do ... While or the execution of the switch structure.

A break can accept an optional numeric parameter to decide to jump out of several loops.

Code:
Copy Code code as follows:

$arr = Array (' One ', ' two ', ' three ', ' four ', ' Stop ', ' five ');
while (the 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 the next loop in the loop structure used to skip the remaining code in this loop and begin execution of this loop structure.

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

Continue accepts an optional numeric parameter to decide to skip several cycles to the end of the loop.

Code:
Copy Code code as follows:

<code>
<?php
while (the 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>

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

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.