Home  >  Article  >  Backend Development  >  Detailed explanation of the differences between break, continue, return, and exit in PHP

Detailed explanation of the differences between break, continue, return, and exit in PHP

烟雨青岚
烟雨青岚forward
2020-07-18 11:48:332841browse

Detailed explanation of the differences between break, continue, return, and exit in PHP

1. The function of return statement

(1) return exits from the current method and returns to the statement of the called method , continue execution.

(2) return Returns a value to the statement that calls the method. The data type of the return value must be consistent with the type of the return value in the declaration of the method.

(3) Return can also be followed by no parameters. Without parameters, it returns empty. In fact, the main purpose is to interrupt the execution of the function and return to the calling function.

2. The role of the break statement

(1) break is in the loop body, forcibly ending the execution of the loop, that is, ending the entire loop process, and no longer determines the execution of the loop Whether the condition is true or not, go directly to the statement below the loop statement.

(2) When break appears in the switch statement body in the loop body, its function is just to jump out of the switch statement body.

3. The role of the continue statement

Terminates the execution of this loop, that is, skips the statements that have not been executed after the continue statement in the current loop, and then proceeds to the next step Judgment of a loop condition.

4. The role of the exit function

exit is a function that exits all scripts.

For specific codes, please refer to:

<?php  
/* php的break,continue,return 的简单区别代码 */  
$i= 1;    
while(true) {// 这里看上去这个循环会一直执行    
    if($i==2) {// 2跳过不显示    
        $i++;    
        continue;    
    }elseif($i==5) {// 但到这里$i=5就跳出循循环了    
        break;    
    }else{    
        echo$i.&#39;<br>&#39;;    
    }    
    $i++;    
}    
exit;          
echo&#39;这里不输出&#39;;
?>

Thank you for reading, I hope you will benefit a lot.

Recommended tutorial: "php tutorial"

The above is the detailed content of Detailed explanation of the differences between break, continue, return, and exit in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete