PHP Control Structure

Source: Internet
Author: User
Tags terminates

1.break jump out of the code snippet to end this loop

2.continue ends the current clip, ends the cycle, and continues the next cycle

3.exit end of entire PHP code

The function of break is to jump out of this loop (if this break or continue inside the IF statement in the loop, not jump out of the IF statement, but jump out of the loop statement), execute the loop after the curly brace statement,
Break is the case in a looping statement, as is the case with the switch statement, and continue is executed when the condition is satisfied without executing the statement following the loop from the beginning of the loop.
1<?PHP2 /*Simple Difference code for PHP's Break,continue,return*/  3 $i= 1; 4  while(true) {//It looks like this loop is going to be executed .5     if($i==2) {//2 Skip not showing6         $i++; 7         Continue; 8}Else if($i==5) {//But when we get here, $i=5 jumps out of circulation.9          Break; Ten}Else {     One         Echo $i. ' <br> ';  A     }     -     $i++;  - }     the Exit;  -        - Echo' No output here '; -?>

return, break, and contiue are language constructs, just like an if statement, but exit is a function.

Let's start with the use of the Exit Function:
Function: Output a message and terminate the current script outputs a messages and terminates the existing scripts.

Exit all scripts If a text contains multiple scripts that start with <?php and end with?>.

For example, if a PHP text includes code, it is not exported to world.

1 <? PHP 3 Echo "Hello"; 5 Exit ; 7 ?> 8  9 <? PHP  One 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 just before exiting.

If status is a string, this function prints the status before the script exits.

If status is a integer, that value would also be used as the exit status. Exit statuses should be in the range 0 to 254, the exit status 255 are reserved by PHP and shall not being used. The status 0 is used to terminate the program successfully.

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.

Break

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

Break can accept an optional numeric parameter to decide to jump out of a few loops.

<?php
$arr = Array (' One ', ' one ', ' one ', ' three ', ' four ', ' Stop ', ' five ');
while (list (, $val) = each ($arr)) {
if ($val = = ' Stop ') {
Break /* You could also write ' break 1; */
}
echo "$val <br/>";
}

/* Use optional Parameters */

$i = 0;
while (+ + $i) {
Switch ($i) {
Case 5:
echo "at 5<br/>";
Break 1; /* Exit switch only. */
Case 10:
echo "at 10; Quitting<br/> ";
Break 2; /* Exit switch and while loop */
Default
Break
}
}
?>

Continue

Continue is used in the loop structure to skip the remaining code in this loop and start the next loop when the condition evaluates to True.

Note: Notice that the switch statement in PHP is considered a looping structure that can use continue.

Continue accepts an optional numeric parameter to decide to skip a few loops to the end of the loop.

<?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/>";
while (1) {
echo "&nbsp;&nbsp; Middle<br/> ";
while (1) {
echo "&nbsp;&nbsp;inner<br/>";
Continue 3;
}
echo "This never gets output.<br/>";
}
echo "Neither does this.<br/>";
}
?>

Omitting the semicolon after continue causes confusion. The following examples indicate that this should not be done.

<?php
  for ($i = 0; $i < 5; ++$i) {
      if ($i == 2)
          continue
      print "$i ";
  }
?>

The desired result is:

0134

The actual output can be:

2

Because the return value of the print () call is int (1), it appears as the optional numeric parameter above.

Return

If you call the return () statement in a function, the execution of the function is terminated immediately and its arguments are returned as the value of the function. Return () also terminates the Eval () statement or the execution of the script file.

If called in the global scope, the current script file aborts the run. If the current script file is either include () or require (), the control is returned to the calling file. Also, if the current script is include (), the value of return () is treated as the return value of the include () call. If you call return () in the main script file, the script aborts the run. If the current script file is php.ini specified in the configuration option Auto_prepend_file or Auto_append_file, then this script file aborts the run.

For more information, see Return values.

Note: Since return () is a language structure and not a function, it is not necessary to enclose the parameter in parentheses. Usually without parentheses, you should actually not use it, which can reduce the burden on PHP.

Note: Never use parentheses when returning a value with a reference, which is not feasible. A variable can only be returned by reference, not the result of the statement. If you use return ($a); Instead of returning a variable, it is the value of the expression ($a) (and, of course, the value at that point $a ).

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.