Difference and usage of return, exit, break and contiue in PHP _ PHP Tutorial

Source: Internet
Author: User
Differences and usage of return, exit, break, and contiue in PHP. Let's talk about the exit function usage first. Purpose: output a message and terminate the current script. If a piece of text contains multiple ending scripts, exit from the current script. Let's talk about the exit function usage first.
Purpose: output a message and terminate the current script.
If a piece of text contains multiple ending scripts, exit from the current script.
For example, if a php text includes the code, 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 status before the script exits.
If status is an integer, the integer is used as the exit status. The exit status should be from 0 to 254. exit status 255 is retained and disabled by PHP. Status 0 is used to indicate successful termination of the program.
Return language structure usage
Purpose: terminate the execution of a function and return a value from the function.
Break and continue are used in the for, foreach, while, do... while or switch structure.

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

The break can accept an optional numeric parameter to determine the number of loops to jump out.

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 the loop structure to skip the remaining code in this loop and start to execute the next loop of this loop structure.

Note: In PHP, the switch statement is considered as a loop structure for the purpose of continue.

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

Code:

The code is 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 article is from the Internet and its source is unknown.

Bytes. Purpose: output a message and terminate the current script. If a piece of text contains multiple ending scripts, exit from the current script. Than...

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.