What are the method statements for breaking out of loops in PHP? (Picture + video tutorial)

藏色散人
Release: 2019-11-02 18:01:40
Original
6290 people have browsed it

For job seekers who are about to enter a PHP position, in addition to self-introduction of relevant experience during the interview process, it is more of a test to answer PHP interview questions. This article will introduce to you a common question during the PHP interview process, that is, what are the methods for PHP to break out of the loop?

Everyone should know that common PHP loop structures include for loop, foreach loop, do...while loop, etc..

Below we will explain it in detail with a for loop example.

php code examples of the four methods to jump out of the for loop are as follows:

First method: continue

<?php
for ($i = 1; $i < 10; $i++) {
    if ($i == 1 || $i == 5) {
      continue;   
    } else {
        echo "$i<br>";
    }
}
Copy after login

When the value of variable $i is equal to 1 or equal to 5, execute the continue statement, otherwise output $i. Then access it through the browser, and the result is as follows:

What are the method statements for breaking out of loops in PHP? (Picture + video tutorial)

As you can see from the picture, all numbers except 1 and 5 are output and displayed. Because continue is in a loop structure, it means giving up the current loop and continuing to the next loop statement. Note that continue itself does not jump out of the loop structure.

Second method: break

<?php
for ($i = 1; $i < 10; $i++) {
    if ($i == 1 || $i == 5) {
      break;   
    } else {
        echo "$i<br>";
    }
}
Copy after login

This code is accessed through the browser, and the page does not display any numbers. We won’t take screenshots here. You can directly copy and paste this code to test locally.

Note, break is in the loop body, which means to forcefully end the loop statement and jump out of the current loop body.

The third method: exit

<?php
for ($i = 1; $i < 10; $i++) {
    if ($i == 1 || $i == 5) {
      exit;   
    } else {
        echo "$i<br>";
    }
}
Copy after login

The same result as above, no numbers are displayed. Note that exit terminates the execution of all script programs, and the code after exit will not be output!

The fourth method: return

<?php
for ($i = 1; $i < 10; $i++) {
    if ($i == 1 || $i == 5) {
      return;   
    } else {
        echo "$i<br>";
    }
}
Copy after login

return means exiting from the current loop, returning to the statement of the called method, and continuing execution.

Note, when return is followed by parameters, there will be a return value; when return is followed by empty, the return is empty.

The above is an introduction to the four methods of PHP jumping out of the loop. It has certain reference value. I hope it will be helpful to friends in need!

If you want to know more about PHP, you can follow the PHP video tutorial on the PHP Chinese website. Everyone is welcome to refer to and learn!

The above is the detailed content of What are the method statements for breaking out of loops in PHP? (Picture + video tutorial). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!