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...