PHP로 계속하기

WBOY
풀어 주다: 2024-08-28 17:46:57
원래의
499명이 탐색했습니다.

Continue 문은 진행 중인 루프의 나머지 반복을 건너뛰고 조건 평가에서 실행을 계속하고 다음 실행 시작으로 점프하도록 코드에 지시하기 위해 조건문 내에서 사용되는 문입니다. PHP에서는 스위치 문에서 continue 문이 사용되는 가장 일반적인 위치입니다.

무료 소프트웨어 개발 과정 시작

웹 개발, 프로그래밍 언어, 소프트웨어 테스팅 등

Continue는 실행이 건너뛰고 끝까지 가야 하는 내부 루프의 수준 수를 정의하는 선택적 숫자 매개 변수를 사용합니다. 즉, 값이 1이면 현재 루프의 끝으로 건너뜁니다. break 문은 루프를 완전히 끝내는 데 사용되는 반면, continue는 현재 루프에서 다음 루프로 이동하는 지름길로 사용됩니다. 둘 다 루프의 코더에게 추가 제어 기능을 제공하여 원하는 대로 조작할 수 있습니다.

구문:

while ( condition 1)
{
//declaration statements
if ( condition 2 )
{
//PHP code goes here
continue ;
}
// Operational Statements
}
로그인 후 복사

위에서 볼 수 있듯이 먼저 조건과 함께 while 루프를 선언합니다. 조건이 만족되면 코드는 루프에 들어가고 조건이 참이 아닐 때까지 계속됩니다. 코드는 조건이 true인 경우에만 if 문 루프에 들어갑니다. 여기서 continue 문은 이 반복을 건너뛰고 continue 이후 코드의 뒷부분을 실행하지 않습니다. 따라서 제어는 조건 1을 갖는 while 루프의 다음 반복으로 전달됩니다.

흐름도

다음은 PHP의 Continue 흐름도입니다.

PHP로 계속하기

흐름도에서 볼 수 있듯이 continue는 루프가 실행되는 코드 내부의 명령문 블록에 항상 존재합니다. 계속 조건이 true인 경우 후속 작업을 건너뛰고 다음 반복으로 건너뜁니다.

PHP에서 Continue의 예

아래에 표시된 몇 가지 자세한 예를 통해 continue 문의 정확한 작동을 살펴보겠습니다.

예시 #1

코드:

<?php
$a = 0;
for ($a = 0;$a <= 10;$a++)
{
if ($a==4)
{
break;
}
echo $a;
echo "\n";
}
echo "End of for loop" ;
?>
로그인 후 복사

출력:

PHP로 계속하기

이 프로그램에서는 먼저 변수 a의 값을 0으로 초기화합니다. 그런 다음 for 루프를 사용하여 값이 10에 도달할 때까지 값을 1씩 증가시킵니다. a의 값이 4에 도달하면 이 루프를 중단하는 데 루프도 사용되는 경우 또 다른 조건문입니다. 따라서 루프를 중단하고 그런 다음 값 5를 인쇄하는 대신 "루프 끝"을 인쇄합니다. 더 잘 이해하기 위해 출력에서 ​​각 증분 값을 인쇄합니다. 보시다시피 break 문 때문에 출력이 4에서 멈춥니다.

이제 continue 문이 동일한 코드에 대해 어떤 역할을 하는지 살펴보겠습니다.

코드:

<?php
$a = 0;
for ($a = 0;$a <= 10;$a++)
{
if ($a==4)
{
continue;
}
echo $a;
echo "\n";
}
echo "End of for loop" ;
?>
로그인 후 복사

출력:

PHP로 계속하기

위 출력에서 ​​볼 수 있듯이 프로그램은 이전 출력과 마찬가지로 0부터 4까지의 숫자를 인쇄했습니다. if 조건문에 이르렀을 때 TRUE인 경우 계속 문으로 인해 인쇄 4를 건너뛰고 루프를 다시 계속했습니다. 따라서 출력에서 ​​4를 볼 수 없지만 for 루프 조건인 5에서 10까지 증가가 다시 시작된 다음 for 루프에서 벗어났음을 나타내는 "End of for loop"가 인쇄됩니다. 따라서 계속은 일반적으로 특정 인스턴스에서 건너뛰고 다음 인스턴스에서 루프를 계속하는 데 사용됩니다.

예시 #2

코드:

<?php
$a=0;
foreach ($a as $k => $val) {
if (!($k % 2)) { // skip even members
continue;
}
do_something_odd($val);
}
$j = 0;
while ($j++ < 5) {
echo "Outermost while loop\n";
while (1) {
echo "Middle while loop\n";
while (1) {
echo "Innermost while loop\n";
continue 3;
}
echo "This text will never get printed\n";
}
echo "This text also will never get printed\n";
}
?>
로그인 후 복사

출력:

PHP로 계속하기

위 코드에서는 모듈로 2 함수를 사용하여 짝수를 건너뛰기 위해 if 조건 루프에서 변수 k를 사용하고 있습니다. 여기서는 현재 반복을 건너뛰는 continue 문을 사용하므로 여기에는 출력이 인쇄되지 않습니다.

루프의 두 번째 부분에서는 변수 j를 사용하고 먼저 이를 0으로 초기화합니다. 그런 다음 3개의 while 루프(가장 바깥쪽, 중간 및 가장 안쪽 루프)를 사용합니다. 가장 바깥쪽 루프에는 변수 j가 5보다 작고 j가 계속 1씩 증가할 때 실행되는 조건이 있습니다. 중간 루프와 내부 루프는 무한하므로 조건 없이 계속 실행됩니다.

Just like the break statement, we can also pass numeric parameters along with the continue statement. As shown in the above example, we are passing the number 3 which means that it skips 3 iterations of the loop. Hence the text which will never get printed is shown in the code. The same code when we give continue 2 will skip the innermost and the middle while loop and prints the last text. Similarly, continue 1 will skip only the innermost loop. However, note that continues 0 is not a valid statement to execute even though it was valid before as it was considered to be the same as continue 1.

Example #3

Continue can also be used in a switch statement as follows:

Code:

<?php
echo"\n";
echo"\n";
for ( $a = 0; $a < 5; $a++ ) {
switch ($a)
{
case 0:
echo $a . "\nb\n";
continue 2;
echo $a . "\na\n";
case 1:
echo $a . "\nb\n";
continue 2;
echo $a . "\na\n";
case 2:
echo $a . "\nb\n";
break;
echo $a . "\na\n";
}
echo "end\n";
}
echo"\n";
echo"\n";
?>
로그인 후 복사

Output:

PHP로 계속하기

It can be seen that in a switch statement, break and continue statements work in the same way. But in the loops, they are used to stop the loop iteration and move on to the next. By using continue 2 we can come out of the switch inside a loop and continue to the next outer loop iteration.

Importance of Continue in PHP

  1.  The main purpose of the continue statement is inside loops for skipping a particular instance of iteration where the coder wants it.
  2. In case of a do-while loop, the continue statement takes control of the conditional statement part of the while loop.
  3. Even in a while loop, the continue again takes control of the condition of while loop.
  4. In for loop, the increment or decrement actions get executed after the continue statement.

Disadvantages of Continue in PHP

  1. We cannot use continue in a file name which is inside a loop as it will cause an error.
  2. Using continue makes the code hard to follow and does not look elegant.

Conclusion – Continue in PHP

Continue statements are majorly used in all kinds of loops or conditional statements in PHP. They basically stop the ongoing iteration of that loop but does not terminate the same. Continue statement present inside the statement block just hops on to the next iteration which succeeds in the conditional statement.

위 내용은 PHP로 계속하기의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
php
원천:php
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!