Detailed explanation of the function and usage of break keyword in PHP

WBOY
Release: 2023-06-28 18:42:01
Original
1930 people have browsed it

Detailed explanation of the role and usage of the break keyword in PHP

In PHP programming, break is a control flow statement that is used to interrupt the current loop or switch statement and jump out of the loop or switch. This article will introduce in detail the role and usage of the break keyword.

1. Break in the loop

In the loop structure, the function of break is to terminate the loop early and jump out of the loop body to execute the code after the loop. Common loop structures include for, while and do...while.

  1. Using break in a for loop

The for loop is a commonly used loop structure that can repeatedly execute a specific block of code based on specified conditions.

The following is an example of calculating the sum of elements in an array through a for loop and terminating the loop early when a certain condition is met:

$nums = [1, 2, 3, 4, 5];
$sum = 0;

for ($i = 0; $i < count($nums); $i++) {
    if ($nums[$i] == 3) {
        break;
    }
    $sum += $nums[$i];
}

echo "数组中元素之和为:" . $sum;  // 输出:数组中元素之和为:3
Copy after login

In the above example, when $i is equal to 2 , the condition $nums[$i] == 3 is met, the break keyword is executed, and the loop is jumped out. Therefore, the sum of the elements in the final output array is 3.

  1. Using break in a while loop

The while loop is a loop structure that repeatedly executes a block of code when a specified condition is true. The following is an example of using a while loop to calculate the sum of positive integers starting from 1, and terminating the loop early when the sum is greater than 10:

$num = 1;
$sum = 0;

while ($sum <= 10) {
    $sum += $num;
    $num++;
    
    if ($sum > 10) {
        break;
    }
}

echo "和大于10时最后一个数字为:" . ($num - 1);  // 输出:和大于10时最后一个数字为:5
Copy after login

In the above example, when the value of $sum is greater than 10, the break keyword Execute and break out of the loop. Therefore, the last number such that the sum is greater than 10 is 5.

  1. Use break in do...while loop

do...while loop executes the loop body once before judging whether the condition is true. If the condition If true, continue executing the loop, otherwise exit the loop.

The following is an example of using a do...while loop to calculate the sum of integers between 1 and n, and terminating the loop early when the sum is greater than 20:

$n = 1;
$sum = 0;

do {
    $sum += $n;
    $n++;

    if ($sum > 20) {
        break;
    }
} while ($n <= 10);

echo "和大于20时最后一个数字为:" . ($n - 1);  // 输出:和大于20时最后一个数字为:6
Copy after login

In the above example, When the value of $sum is greater than 20, the break keyword is executed and the loop is jumped out. Therefore, the last number such that the sum is greater than 20 is 6.

2. Break in the switch statement

In PHP, the switch statement is used to execute different code blocks according to different conditions. The break keyword is used in the switch statement to jump out and end the code execution in the switch block.

The following is an example that uses the switch statement to calculate the price of fruit based on the incoming parameters and returns the final price:

$fruit = "apple";
$price = 0;

switch ($fruit) {
    case "apple":
        $price = 5;
        break;
    case "banana":
        $price = 3;
        break;
    case "pear":
        $price = 4;
        break;
    default:
        $price = 0;
        break;
}

echo "您选择的水果价格为:" . $price;  // 输出:您选择的水果价格为:5
Copy after login

In the above example, based on the value of $fruit, the corresponding case is executed statement and assign the corresponding price to the $price variable. When the case statement is executed, the break keyword will be encountered and the execution of the switch block will be jumped out.

If the break keyword is not used in a case statement, it will cause the program to continue executing the next case statement until the break keyword is encountered or the switch block ends.

Summary:

In PHP programming, the break keyword is used to terminate the execution of loops and switch statements early. Using break in a loop can jump out of the loop body, avoid unnecessary code execution, and improve program execution efficiency. Using break in a switch statement can ensure that only case statements that meet the conditions are executed and avoid continuing to execute the next case statement. When using the break keyword, you need to pay attention to the control flow logic to ensure that the program can be executed correctly.

The above is the detailed content of Detailed explanation of the function and usage of break keyword in PHP. For more information, please follow other related articles on the PHP Chinese website!

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!