首页  >  文章  >  后端开发  >  PHP 的破解

PHP 的破解

WBOY
WBOY原创
2024-08-29 12:40:451119浏览

PHP的break语句用于立即退出循环,而无需等待返回for循环、while循环、do-while、switch和for-each循环等条件语句。如果存在多个循环并且使用了break语句,则它仅从第一个内部循环退出。 Break 存在于语句块内,并为用户提供了在需要时退出循环的完全自由。

广告 该类别中的热门课程 财务建模和估值 - 专业化 | 51 课程系列 | 30 次模拟测试

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

语法:

<?php
//variable declarations
//conditional statement block
{
break;
}
?>

流程图

PHP 的破解

如上所示,当循环的条件满足时,代码首先进入条件语句块,并不断执行循环中的语句,直到条件不满足为止。当代码中写入break语句时,一旦程序遇到它,无论条件是否满足,代码都会退出当前循环,如图所示。

PHP 中断示例

让我们通过为不同场景中的每个条件语句举几个示例并检查其行为来了解break语句的工作原理。

示例#1

“for”循环内的 Break 语句。

代码:

<?php
$var = 1;
for ($var = 0;$var <= 10;$var++)
{
if ($var==5)
{
break;
}
echo $var;
echo "\n";
}
echo "For loop ends here" ;
?>

输出:

PHP 的破解

这里我们通过将 1 初始化为变量“var”,在 for 循环中打印从 1 到 10 的数字。 “var”开始打印从 1 开始的增量数字,直到遇到 if 循环条件。这里我们提到,一旦变量的值达到 5,它就应该退出循环。这是使用如图所示的break语句完成的。我们可以在输出中看到相同的内容,因为一旦执行了break语句,即使不满足for循环条件,它也会从for循环中打印“For循环结束于此”。因此,break 语句来自所有其他迭代的整个逻辑。

示例#2

此示例是检查 while 循环中的break语句的功能。

代码:

<?php
$var = 0;
while( $var < 10) {
$var++;
if( $var == 5 )break;
echo ("Current variable value = $var");
echo "\n";
}
echo ("Exited loop at variable value = $var" );
?>

输出:

PHP 的破解

在上面的程序中,变量“var”首先被初始化为 0,然后使用 while 循环将其值增加 1 并打印相同的值。我们正在编写一个 if 条件,其中一旦变量值等于 5,我们就使用 Break 语句使代码退出。此中断使其从当前 while 循环退出,即使不满足递增变量直到值 10 的指定条件。遇见了。我们正在显示循环中断时的变量值。

示例#3

这里我们在 foreach 循环中实现break语句。

代码:

<?php
$array = array('A', 'B', 'C', 'D', 'E', 'F');
foreach ($array as $let) {
if ($let == 'E') {
break;
}
echo "$let \n";
}

输出:

PHP 的破解

在这个程序中,我们首先声明一个包含字母集合的数组。然后通过使用 foreach 循环,我们一一打印数组的所有元素。一旦数组指针的值到达字母“E”,引入 if 条件语句来中断循环。因此,在遇到break语句时,代码退出而不打印数组中的下一个字母,即“F”。

示例#4

break 最常见的应用是在 switch 语句中,如下所示。

代码:

<?php
$a=1;
switch ($a) {
case 0:
echo "a equals 0";
break;
case 1:
echo "a equals 1";
break;
case 2:
echo "a equals 2";
break;
}
?>

输出:

PHP 的破解

这是一个简单的 switch 语句的示例,我们首先将变量值初始化为 1。然后通过使用 switch 条件,我们检查变量值并在条件匹配时打印它。

示例#5

这里让我们看看当有两个或多个循环(条件语句)时,break 语句的工作原理。

代码:

<?php
// PHP program to verify break of inner loop
// Declaration of 2 arrays as below
$array1 = array( 'One', 'Two', 'Three' );
$array2 = array( 'Three', 'One', 'Two', 'Four' );
// Outer foreach loop
foreach ($array1 as $a1) {
echo "$a1 ";
// Inner nested foreach loop
foreach ($array2 as $a2) {
if ($a1 != $a2 )
echo "$a2 ";
else
break 2;
}
echo "\n";
}
echo "\n Loop Terminated";
?>

输出:

PHP 的破解

Here we are using 2 nested foreach loops and also showing a case of using “break 2” which breaks out of both the loops in contrast to the “break” statement which breaks out of only the inner loop.

We have declared two arrays array1 and array2 and we are displaying the values of array2 for each value of array1 until and unless the value of array1 is not equal to array2. Once the value in array1 becomes the same as array2 we are breaking both loops by making use of break 2 statement which prevents the code from executing any more statements.

Example #6

Here we shall see how to use break statements to come out of “n” number of loops (conditional statements).

Code:

<?php
## Outermost first for loop
for($a=1;$a<5;$a++){
echo ("Value of a is = $a");
echo "\n";
## Second for loop
for($b=1;$b<3;$b++){
echo ("Value of b is = $b");
echo "\n";
## Third for loop
for($c=2;$c<3;$c++){
echo ("Value of c is = $c");
echo "\n";
## Fourth for loop
for($d=2;$d<4;$d++){
echo ("Value of d is = $d");
echo "\n";
if( $a == 3 ){
break 4;
}
}
}
}
}
echo 'Loop has terminated and value of a = '.$a;
?>

Output:

PHP 的破解

The break statement followed by the number of loops that need to be exited from are used to terminate from “n” number of loops.

Syntax:

break n;

where n is the number of loops that need to be exited from the loop.

We are using 4 nested for loops for this purpose. Variables a, b, c, d is initialized respectively for each for loop and we are incrementing their values by 1. The same values are displayed in the output to understand its flow better. At last, we are giving a condition for all the 4 loops to break if the value of the first variable becomes equal to 3 which it eventually did and can be shown in the output. We are printing the loop has terminated at the end along with as value to note the break’s functionality.

Conclusion

When we are using one or more conditional statements in our code and we need to exit from the same at a particular point, we can use the break statement. Basically, it helps to terminate from the code when the condition we give falls TRUE. We can also pass an integer along with break to terminate from any number of existing loops instead of declaring the break again and again.

以上是PHP 的破解的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:PHP Switch Statement下一篇:PHP Loops