Hướng dẫn php break out of if - php thoát ra khỏi nếu

Có bất kỳ lệnh nào trong PHP để ngừng thực thi câu lệnh

do {
  
  if( !process_x() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  
  if( !process_y() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  
  if( !process_z() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  /* SUCCESS */
  
} while (0);
2 hiện tại hoặc cha mẹ, giống như
do {
  
  if( !process_x() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  
  if( !process_y() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  
  if( !process_z() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  /* SUCCESS */
  
} while (0);
3 hoặc
do {
  
  if( !process_x() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  
  if( !process_y() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  
  if( !process_z() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  /* SUCCESS */
  
} while (0);
4 cho ________ 35/________ 36. Ví dụ

$arr=array('a','b');
foreach($arr as $val)
{
  break;
  echo "test";
}

echo "finish";

Trong mã trên, PHP sẽ không làm

do {
  
  if( !process_x() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  
  if( !process_y() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  
  if( !process_z() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  /* SUCCESS */
  
} while (0);
7 và sẽ chuyển đến
do {
  
  if( !process_x() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  
  if( !process_y() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  
  if( !process_z() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  /* SUCCESS */
  
} while (0);
8

Tôi cần cái này cho nếu

$a="test";
if("test"==$a)
{
  break;
  echo "yes"; // I don't want this line or lines after to be executed, without using another if
}
echo "finish";

Tôi muốn

do {
  
  if( !process_x() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  
  if( !process_y() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  
  if( !process_z() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  /* SUCCESS */
  
} while (0);
3 Tuyên bố
do {
  
  if( !process_x() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  
  if( !process_y() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  
  if( !process_z() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  /* SUCCESS */
  
} while (0);
2 ở trên và ngừng thực thi
try{
    if( !process_x() ){
        throw new Exception('process_x failed');
    }

    /* do a lot of other things */

    if( !process_y() ){
        throw new Exception('process_y failed');
    }

    /* do a lot of other things */

    if( !process_z() ){
        throw new Exception('process_z failed');
    }

    /* do a lot of other things */
    /* SUCCESS */
}catch(Exception $ex){
    clean_all_processes();
}
1 hoặc các mã đó không còn cần thiết để được thực thi, có thể có hoặc không có điều kiện bổ sung, có cách nào để làm điều này không?

CẬP NHẬT: Chỉ 2 năm sau khi đăng câu hỏi này, tôi lớn lên, tôi đã học được cách viết mã bằng các đoạn nhỏ, tại sao lại lồng nhau có thể là mùi mã và làm thế nào để tránh những vấn đề như vậy ngay từ đầu bằng cách viết các chức năng nhỏ, có thể quản lý được.

Hỏi ngày 19 tháng 9 năm 2011 lúc 9:29Sep 19, 2011 at 9:29

Hướng dẫn php break out of if - php thoát ra khỏi nếu

Muhammad Usmanmuhammad UsmanMuhammad Usman

12.3k6 Huy hiệu vàng35 Huy hiệu bạc58 Huy hiệu Đồng6 gold badges35 silver badges58 bronze badges

5

Đôi khi, khi phát triển những thứ "ưa thích" này là bắt buộc. Nếu chúng ta có thể phá vỡ

do {
  
  if( !process_x() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  
  if( !process_y() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  
  if( !process_z() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  /* SUCCESS */
  
} while (0);
2, rất nhiều ____32 lồng nhau sẽ không cần thiết, làm cho mã sạch hơn và thẩm mỹ hơn nhiều.

Mã mẫu này minh họa rằng trong một số tình huống nhất định,

do {
  
  if( !process_x() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  
  if( !process_y() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  
  if( !process_z() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  /* SUCCESS */
  
} while (0);
2 bị phá vỡ có thể phù hợp hơn nhiều so với rất nhiều ____32 được lồng xấu xí.

Mã xấu xí

if(process_x()) {

    /* do a lot of other things */

    if(process_y()) {

         /* do a lot of other things */

         if(process_z()) {

              /* do a lot of other things */
              /* SUCCESS */

         }
         else {

              clean_all_processes();

         }

    }
    else {

         clean_all_processes();

    }

}
else {

    clean_all_processes();

}

Mã đẹp mắt

do {
  
  if( !process_x() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  
  if( !process_y() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  
  if( !process_z() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  /* SUCCESS */
  
} while (0);

Như @niematojaktomasz nói, việc sử dụng

try{
    if( !process_x() ){
        throw new Exception('process_x failed');
    }

    /* do a lot of other things */

    if( !process_y() ){
        throw new Exception('process_y failed');
    }

    /* do a lot of other things */

    if( !process_z() ){
        throw new Exception('process_z failed');
    }

    /* do a lot of other things */
    /* SUCCESS */
}catch(Exception $ex){
    clean_all_processes();
}
6 là một giải pháp thay thế, điều xấu về điều này là bạn luôn cần xác định nhãn (mục tiêu điểm).

Hướng dẫn php break out of if - php thoát ra khỏi nếu

Adriaan

17.6K7 Huy hiệu vàng37 Huy hiệu bạc73 Huy hiệu đồng7 gold badges37 silver badges73 bronze badges

Đã trả lời ngày 15 tháng 11 năm 2011 lúc 1:30Nov 15, 2011 at 1:30

Hướng dẫn php break out of if - php thoát ra khỏi nếu

AgelessessenceaglessessenceAgelessEssence

6.1055 huy hiệu vàng31 Huy hiệu bạc36 Huy hiệu đồng5 gold badges31 silver badges36 bronze badges

10

Đóng gói mã của bạn trong một chức năng. Bạn có thể ngừng thực hiện một chức năng với

try{
    if( !process_x() ){
        throw new Exception('process_x failed');
    }

    /* do a lot of other things */

    if( !process_y() ){
        throw new Exception('process_y failed');
    }

    /* do a lot of other things */

    if( !process_z() ){
        throw new Exception('process_z failed');
    }

    /* do a lot of other things */
    /* SUCCESS */
}catch(Exception $ex){
    clean_all_processes();
}
7 bất cứ lúc nào.

Đã trả lời ngày 19 tháng 9 năm 2011 lúc 9:31Sep 19, 2011 at 9:31

Hướng dẫn php break out of if - php thoát ra khỏi nếu

4

Cách thích hợp để làm điều này:

try{
    if( !process_x() ){
        throw new Exception('process_x failed');
    }

    /* do a lot of other things */

    if( !process_y() ){
        throw new Exception('process_y failed');
    }

    /* do a lot of other things */

    if( !process_z() ){
        throw new Exception('process_z failed');
    }

    /* do a lot of other things */
    /* SUCCESS */
}catch(Exception $ex){
    clean_all_processes();
}

Sau khi đọc một số ý kiến, tôi nhận ra rằng xử lý ngoại lệ không phải lúc nào cũng có ý nghĩa đối với kiểm soát dòng chảy thông thường. Đối với luồng điều khiển thông thường, tốt hơn là sử dụng "nếu khác":

try{
  if( process_x() && process_y() && process_z() ) {
    // all processes successful
    // do something
  } else {
    //one of the processes failed
    clean_all_processes();
  }
}catch(Exception ex){
  // one of the processes raised an exception
  clean_all_processes();
}

Bạn cũng có thể lưu các giá trị trả về quá trình trong các biến và sau đó kiểm tra các khối thất bại/ngoại lệ mà quá trình đã thất bại.

Đã trả lời ngày 7 tháng 1 năm 2014 lúc 22:18Jan 7, 2014 at 22:18

Hướng dẫn php break out of if - php thoát ra khỏi nếu

3

Bởi vì bạn có thể thoát ra khỏi một vòng/trong khi chúng tôi "làm" một vòng. Với một thời gian (sai) ở cuối, điều kiện không bao giờ đúng và sẽ không lặp lại, nữa.break out of a do/while loop, let us "do" one round. With a while(false) at the end, the condition is never true and will not repeat, again.

do
{
    $subjectText = trim(filter_input(INPUT_POST, 'subject'));
    if(!$subjectText)
    {
        $smallInfo = 'Please give a subject.';
        break;
    }

    $messageText = trim(filter_input(INPUT_POST, 'message'));
    if(!$messageText)
    {
        $smallInfo = 'Please supply a message.';
        break;
    }
} while(false);

Đã trả lời ngày 12 tháng 6 năm 2016 lúc 8:18Jun 12, 2016 at 8:18

Markus Zellermarkus ZellerMarkus Zeller

7.6502 Huy hiệu vàng30 Huy hiệu bạc33 Huy hiệu Đồng2 gold badges30 silver badges33 bronze badges

1

goto:

Toán tử Goto có thể được sử dụng để nhảy sang một phần khác trong chương trình. Điểm mục tiêu được chỉ định bởi một nhãn theo sau là dấu hai chấm và hướng dẫn được đưa ra như GOTO theo sau là nhãn mục tiêu mong muốn. Đây không phải là một goto không giới hạn đầy đủ. Nhãn đích phải nằm trong cùng một tệp và bối cảnh, nghĩa là bạn không thể nhảy ra khỏi một hàm hoặc phương thức, bạn cũng không thể nhảy vào một tệp. Bạn cũng không thể nhảy vào bất kỳ loại vòng lặp hoặc cấu trúc chuyển đổi. Bạn có thể nhảy ra khỏi những thứ này, và sử dụng phổ biến là sử dụng goto thay cho một kỳ nghỉ đa cấp ...

Gnat

6.212104 Huy hiệu vàng53 Huy hiệu bạc73 Huy hiệu đồng104 gold badges53 silver badges73 bronze badges

Đã trả lời ngày 19 tháng 9 năm 2011 lúc 9:30Sep 19, 2011 at 9:30

5

Có lệnh tồn tại: try{ if( !process_x() ){ throw new Exception('process_x failed'); } /* do a lot of other things */ if( !process_y() ){ throw new Exception('process_y failed'); } /* do a lot of other things */ if( !process_z() ){ throw new Exception('process_z failed'); } /* do a lot of other things */ /* SUCCESS */ }catch(Exception $ex){ clean_all_processes(); } 6try{ if( !process_x() ){ throw new Exception('process_x failed'); } /* do a lot of other things */ if( !process_y() ){ throw new Exception('process_y failed'); } /* do a lot of other things */ if( !process_z() ){ throw new Exception('process_z failed'); } /* do a lot of other things */ /* SUCCESS */ }catch(Exception $ex){ clean_all_processes(); } 6

if(smth) {
   .....
   .....
   .....
   .....
   .....
   goto My123;
   .....
   .....


}



My123:
....your code here....

NHƯNG HÃY NHỚ!

try{
    if( !process_x() ){
        throw new Exception('process_x failed');
    }

    /* do a lot of other things */

    if( !process_y() ){
        throw new Exception('process_y failed');
    }

    /* do a lot of other things */

    if( !process_z() ){
        throw new Exception('process_z failed');
    }

    /* do a lot of other things */
    /* SUCCESS */
}catch(Exception $ex){
    clean_all_processes();
}
6 không nên được sử dụng ở bất cứ đâu trong các kịch bản trong thế giới thực, vì nó là một dấu hiệu của mã kém.

Đã trả lời ngày 18 tháng 7 năm 2017 lúc 10:08Jul 18, 2017 at 10:08

Hướng dẫn php break out of if - php thoát ra khỏi nếu

T.ToduaT.ToduaT.Todua

50,5K19 Huy hiệu vàng219 Huy hiệu bạc217 Huy hiệu đồng19 gold badges219 silver badges217 bronze badges

1

Bạn có thể sử dụng một trong khi làm việc (sai):

    

Như được mô tả trong http://php.net/manual/en/control-structures.if.php#90073

Đã trả lời ngày 29 tháng 8 năm 2014 lúc 13:26Aug 29, 2014 at 13:26

Hướng dẫn php break out of if - php thoát ra khỏi nếu

UltradeVVultradevvUltraDEVV

Phù hiệu bằng đồng năm 19711 silver badge11 bronze badges

Không, không có cách nào để "phá vỡ" một khối nếu bạn sẽ bên trong các vòng lặp .________ 50 Vì vậy, hãy biến bài kiểm tra của bạn thành

do {
  
  if( !process_x() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  
  if( !process_y() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  
  if( !process_z() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  /* SUCCESS */
  
} while (0);
5!
So turn your test into a
do {
  
  if( !process_x() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  
  if( !process_y() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  
  if( !process_z() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  /* SUCCESS */
  
} while (0);
5 !

Tôi tự hỏi tại sao không ai khuyến khích bạn sử dụng câu lệnh Switch vì (ngay cả khi bạn không có nhiều trường hợp thử nghiệm) Bạn có nghĩ rằng đó là quá dài dòng không? (even if you haven't to many test cases)
Do you think it's too verbose?

Tôi chắc chắn sẽ đi cho nó ở đây

  switch($a){
    case 'test':
        # do stuff here ...
        if(/* Reason why you may break */){
           break; # this will prevent executing "echo 'yes';" statement
        }
        echo 'yes';  # ...           
        break; # As one may already know, we might always have to break at the end of case to prevent executing following cases instructions.
    # default:
        # something else here  ..
        # break;
  }

Đối với tôi các ngoại lệ có nghĩa là để nêu lỗi và không thực sự kiểm soát lỗ hổng thực thi. Nếu hành vi phá vỡ bạn đang cố gắng đặt không phải là về (các) lỗi bất ngờ, việc xử lý ngoại lệ không phải là giải pháp phù hợp ở đây

try{
  if( process_x() && process_y() && process_z() ) {
    // all processes successful
    // do something
  } else {
    //one of the processes failed
    clean_all_processes();
  }
}catch(Exception ex){
  // one of the processes raised an exception
  clean_all_processes();
}
2.
If the break behaviour you are trying to set is not about unexpected error(s), Exception handling is not the right solution here
try{
  if( process_x() && process_y() && process_z() ) {
    // all processes successful
    // do something
  } else {
    //one of the processes failed
    clean_all_processes();
  }
}catch(Exception ex){
  // one of the processes raised an exception
  clean_all_processes();
}
2.

Đã trả lời ngày 5 tháng 6 năm 2014 lúc 8:39Jun 5, 2014 at 8:39

StphanestphaneStphane

3.3065 huy hiệu vàng33 Huy hiệu bạc47 Huy hiệu đồng5 gold badges33 silver badges47 bronze badges

4

$a="test";
if("test"==$a)
{
  break;
  echo "yes"; // I don't want this line or lines after to be executed, without using another if
}
echo "finish";
0

Bằng cách này, tôi đã có những gì tôi muốn. Tôi chỉ sử dụng một công tắc có một trường hợp xác định và sau đó sử dụng ngắt trong trường hợp để chọn nếu điều kiện. Lý do tại sao tôi sử dụng Break: điều kiện1 và điều kiện2 có thể thỏa mãn, trong tình huống đó, chỉ có điều kiện1 được áp dụng. Nếu được chọn lọc theo thứ tự.

Hướng dẫn php break out of if - php thoát ra khỏi nếu

Tobias Mühl

1.6491 Huy hiệu vàng18 Huy hiệu bạc 30 Huy hiệu Đồng1 gold badge18 silver badges30 bronze badges

Đã trả lời ngày 7 tháng 7 năm 2014 lúc 15:25Jul 7, 2014 at 15:25

Hướng dẫn php break out of if - php thoát ra khỏi nếu

2

No.

Nhưng làm thế nào về:

$a="test";
if("test"==$a)
{
  break;
  echo "yes"; // I don't want this line or lines after to be executed, without using another if
}
echo "finish";
1

Đã trả lời ngày 19 tháng 9 năm 2011 lúc 9:30Sep 19, 2011 at 9:30

3

Có lệnh tồn tại:

try{
    if( !process_x() ){
        throw new Exception('process_x failed');
    }

    /* do a lot of other things */

    if( !process_y() ){
        throw new Exception('process_y failed');
    }

    /* do a lot of other things */

    if( !process_z() ){
        throw new Exception('process_z failed');
    }

    /* do a lot of other things */
    /* SUCCESS */
}catch(Exception $ex){
    clean_all_processes();
}
6

Đã trả lời ngày 19 tháng 9 năm 2011 lúc 9:32Sep 19, 2011 at 9:32

MchlmchlMchl

60.7k9 Huy hiệu vàng114 Huy hiệu bạc119 Huy hiệu đồng9 gold badges114 silver badges119 bronze badges

Câu trả lời đơn giản là không, không có cách nào để thoát khỏi câu lệnh

do {
  
  if( !process_x() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  
  if( !process_y() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  
  if( !process_z() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  /* SUCCESS */
  
} while (0);
2 mà không dừng hoàn toàn việc thực thi (thông qua
try{
  if( process_x() && process_y() && process_z() ) {
    // all processes successful
    // do something
  } else {
    //one of the processes failed
    clean_all_processes();
  }
}catch(Exception ex){
  // one of the processes raised an exception
  clean_all_processes();
}
5). Các giải pháp khác sẽ không phù hợp với tôi vì tôi không thể thay đổi cấu trúc của câu lệnh
do {
  
  if( !process_x() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  
  if( !process_y() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  
  if( !process_z() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  /* SUCCESS */
  
} while (0);
2, vì tôi đang tiêm mã vào một plugin, như vậy:

$a="test";
if("test"==$a)
{
  break;
  echo "yes"; // I don't want this line or lines after to be executed, without using another if
}
echo "finish";
2

Đã trả lời ngày 12 tháng 6 năm 2015 lúc 6:13Jun 12, 2015 at 6:13

DaviddavidDavid

5641 Huy hiệu vàng6 Huy hiệu bạc9 Huy hiệu đồng1 gold badge6 silver badges9 bronze badges

Tôi đã từng gặp vấn đề tương tự. Một giải pháp là chất đống nếu. Ví dụ đầu tiên là đơn giản nhưng ...

$a="test";
if("test"==$a)
{
  break;
  echo "yes"; // I don't want this line or lines after to be executed, without using another if
}
echo "finish";
3

Hoặc, bạn có thể sử dụng goto.

$a="test";
if("test"==$a)
{
  break;
  echo "yes"; // I don't want this line or lines after to be executed, without using another if
}
echo "finish";
4

Đã trả lời ngày 4 tháng 11 năm 2021 lúc 18:46Nov 4, 2021 at 18:46

NilavnilavNilav

Huy hiệu 111 Đồng1 bronze badge

Trả lời câu hỏi của bạn liệu điều đó có thể đạt được hay không, thì có điều đó có thể đạt được bằng cách sử dụng toán tử "goto" của PHP.

Nhưng về mặt đạo đức, đó không phải là một cách thực hành tốt để sử dụng "goto" và có bất kỳ nhu cầu nào phải sử dụng goto thì điều này có nghĩa là mã cần phải được xây dựng lại để có thể loại bỏ yêu cầu của goto.

Theo mã mẫu bạn đã đăng ở trên, có thể thấy rõ rằng mã có thể được xây dựng lại và mã không cần thiết hơn có thể bị xóa hoặc nhận xét (nếu khả năng được sử dụng trong tương lai).

Đã trả lời ngày 12 tháng 6 năm 2015 lúc 7:24Jun 12, 2015 at 7:24

$a="test";
if("test"==$a)
{
  break;
  echo "yes"; // I don't want this line or lines after to be executed, without using another if
}
echo "finish";
5

Kết hợp các tuyên bố của bạn, tôi nghĩ rằng điều này sẽ cung cấp cho bạn kết quả mong muốn của bạn. Sạch sẽ và đơn giản, mà không có quá nhiều tuyên bố.

Đối với mã xấu xí và đẹp mắt, sự tái hiện của tôi sẽ là:

$a="test";
if("test"==$a)
{
  break;
  echo "yes"; // I don't want this line or lines after to be executed, without using another if
}
echo "finish";
6

Ở đâu đó trong mã thông thường của bạn

$a="test";
if("test"==$a)
{
  break;
  echo "yes"; // I don't want this line or lines after to be executed, without using another if
}
echo "finish";
7

Đã trả lời ngày 26 tháng 11 năm 2015 lúc 9:36Nov 26, 2015 at 9:36

Tôi có một giải pháp đơn giản mà không có nhiều thay đổi. Tuyên bố ban đầu là

Tôi muốn phá vỡ câu lệnh IF ở trên và ngừng thực hiện Echo "Có"; Hoặc các mã như vậy không còn cần thiết để được thực thi, có thể có hoặc không có điều kiện bổ sung, có cách nào để làm điều này không?

Vì vậy, nó có vẻ đơn giản. Hãy thử mã như thế này.

$a="test";
if("test"==$a)
{
  break;
  echo "yes"; // I don't want this line or lines after to be executed, without using another if
}
echo "finish";
8

Nếu bạn muốn thử mà không cần mã này, thì thật đơn giản. Và bạn có thể quay lại khi bạn muốn. Một giải pháp khác là các khối bình luận. Hoặc chỉ đơn giản là suy nghĩ và thử trong một mã được phân tách khác và chỉ sao chép dán kết quả trong mã cuối cùng của bạn. và nếu một mã không còn nữa, trong trường hợp của bạn, kết quả có thể là

$a="test";
if("test"==$a)
{
  break;
  echo "yes"; // I don't want this line or lines after to be executed, without using another if
}
echo "finish";
9

Với mã này, tuyên bố ban đầu hoàn toàn được tôn trọng .. :) và dễ đọc hơn!

Đã trả lời ngày 31 tháng 12 năm 2015 lúc 3:45Dec 31, 2015 at 3:45

Giải pháp đơn giản là nhận xét nó ra.

if(process_x()) {

    /* do a lot of other things */

    if(process_y()) {

         /* do a lot of other things */

         if(process_z()) {

              /* do a lot of other things */
              /* SUCCESS */

         }
         else {

              clean_all_processes();

         }

    }
    else {

         clean_all_processes();

    }

}
else {

    clean_all_processes();

}
0

Lợi ích được thêm vào là bạn không thay đổi mã ban đầu của bạn và bạn có thể hẹn hò với nó, ban đầu và đặt một lý do tại sao.

Miken32

40.6K16 Huy hiệu vàng100 Huy hiệu bạc142 Huy hiệu đồng16 gold badges100 silver badges142 bronze badges

Đã trả lời ngày 7 tháng 7 năm 2014 lúc 15:36Jul 7, 2014 at 15:36

Hướng dẫn php break out of if - php thoát ra khỏi nếu

ArtisticphoenixartisticphoenixArtisticPhoenix

21.1k2 Huy hiệu vàng21 Huy hiệu bạc35 Huy hiệu Đồng2 gold badges21 silver badges35 bronze badges

3

Điều gì về việc sử dụng toán tử ternary?

if(process_x()) {

    /* do a lot of other things */

    if(process_y()) {

         /* do a lot of other things */

         if(process_z()) {

              /* do a lot of other things */
              /* SUCCESS */

         }
         else {

              clean_all_processes();

         }

    }
    else {

         clean_all_processes();

    }

}
else {

    clean_all_processes();

}
1

Giống hệt với câu lệnh nếu/khác:

if(process_x()) {

    /* do a lot of other things */

    if(process_y()) {

         /* do a lot of other things */

         if(process_z()) {

              /* do a lot of other things */
              /* SUCCESS */

         }
         else {

              clean_all_processes();

         }

    }
    else {

         clean_all_processes();

    }

}
else {

    clean_all_processes();

}
2

Hướng dẫn php break out of if - php thoát ra khỏi nếu

durron597

31.6K16 Huy hiệu vàng98 Huy hiệu bạc154 Huy hiệu đồng16 gold badges98 silver badges154 bronze badges

Đã trả lời ngày 17 tháng 9 năm 2015 lúc 19:39Sep 17, 2015 at 19:39

1

Để ngăn chặn hoàn toàn phần còn lại của tập lệnh chạy, bạn có thể làm

lối ra; // thay cho nghỉ. Phần còn lại của mã sẽ không thực thi

Đã trả lời ngày 3 tháng 5 năm 2017 lúc 14:15May 3, 2017 at 14:15

Hướng dẫn php break out of if - php thoát ra khỏi nếu

Tôi đến bữa tiệc trễ nhưng tôi muốn đóng góp. Tôi ngạc nhiên khi không ai đề nghị

try{
  if( process_x() && process_y() && process_z() ) {
    // all processes successful
    // do something
  } else {
    //one of the processes failed
    clean_all_processes();
  }
}catch(Exception ex){
  // one of the processes raised an exception
  clean_all_processes();
}
7. Nó tốt để thử nghiệm. Tôi sử dụng nó mọi lúc và hoạt động như quyến rũ.

if(process_x()) {

    /* do a lot of other things */

    if(process_y()) {

         /* do a lot of other things */

         if(process_z()) {

              /* do a lot of other things */
              /* SUCCESS */

         }
         else {

              clean_all_processes();

         }

    }
    else {

         clean_all_processes();

    }

}
else {

    clean_all_processes();

}
3

Mã sẽ dừng ở

try{
  if( process_x() && process_y() && process_z() ) {
    // all processes successful
    // do something
  } else {
    //one of the processes failed
    clean_all_processes();
  }
}catch(Exception ex){
  // one of the processes raised an exception
  clean_all_processes();
}
7 và mọi thứ sau khi sẽ không chạy.

Kết quả

if(process_x()) {

    /* do a lot of other things */

    if(process_y()) {

         /* do a lot of other things */

         if(process_z()) {

              /* do a lot of other things */
              /* SUCCESS */

         }
         else {

              clean_all_processes();

         }

    }
    else {

         clean_all_processes();

    }

}
else {

    clean_all_processes();

}
4

Nó cũng hoạt động với

try{
  if( process_x() && process_y() && process_z() ) {
    // all processes successful
    // do something
  } else {
    //one of the processes failed
    clean_all_processes();
  }
}catch(Exception ex){
  // one of the processes raised an exception
  clean_all_processes();
}
9 và
do
{
    $subjectText = trim(filter_input(INPUT_POST, 'subject'));
    if(!$subjectText)
    {
        $smallInfo = 'Please give a subject.';
        break;
    }

    $messageText = trim(filter_input(INPUT_POST, 'message'));
    if(!$messageText)
    {
        $smallInfo = 'Please supply a message.';
        break;
    }
} while(false);
0. Nó hoạt động bất cứ nơi nào bạn đặt nó thực sự.

if(process_x()) {

    /* do a lot of other things */

    if(process_y()) {

         /* do a lot of other things */

         if(process_z()) {

              /* do a lot of other things */
              /* SUCCESS */

         }
         else {

              clean_all_processes();

         }

    }
    else {

         clean_all_processes();

    }

}
else {

    clean_all_processes();

}
5

Kết quả

if(process_x()) {

    /* do a lot of other things */

    if(process_y()) {

         /* do a lot of other things */

         if(process_z()) {

              /* do a lot of other things */
              /* SUCCESS */

         }
         else {

              clean_all_processes();

         }

    }
    else {

         clean_all_processes();

    }

}
else {

    clean_all_processes();

}
6

Nó cũng hoạt động với

try{
  if( process_x() && process_y() && process_z() ) {
    // all processes successful
    // do something
  } else {
    //one of the processes failed
    clean_all_processes();
  }
}catch(Exception ex){
  // one of the processes raised an exception
  clean_all_processes();
}
9 và
do
{
    $subjectText = trim(filter_input(INPUT_POST, 'subject'));
    if(!$subjectText)
    {
        $smallInfo = 'Please give a subject.';
        break;
    }

    $messageText = trim(filter_input(INPUT_POST, 'message'));
    if(!$messageText)
    {
        $smallInfo = 'Please supply a message.';
        break;
    }
} while(false);
0. Nó hoạt động bất cứ nơi nào bạn đặt nó thực sự.

if(process_x()) {

    /* do a lot of other things */

    if(process_y()) {

         /* do a lot of other things */

         if(process_z()) {

              /* do a lot of other things */
              /* SUCCESS */

         }
         else {

              clean_all_processes();

         }

    }
    else {

         clean_all_processes();

    }

}
else {

    clean_all_processes();

}
7

Kết quả

if(process_x()) {

    /* do a lot of other things */

    if(process_y()) {

         /* do a lot of other things */

         if(process_z()) {

              /* do a lot of other things */
              /* SUCCESS */

         }
         else {

              clean_all_processes();

         }

    }
    else {

         clean_all_processes();

    }

}
else {

    clean_all_processes();

}
8

Nó cũng hoạt động với

try{
  if( process_x() && process_y() && process_z() ) {
    // all processes successful
    // do something
  } else {
    //one of the processes failed
    clean_all_processes();
  }
}catch(Exception ex){
  // one of the processes raised an exception
  clean_all_processes();
}
9 và
do
{
    $subjectText = trim(filter_input(INPUT_POST, 'subject'));
    if(!$subjectText)
    {
        $smallInfo = 'Please give a subject.';
        break;
    }

    $messageText = trim(filter_input(INPUT_POST, 'message'));
    if(!$messageText)
    {
        $smallInfo = 'Please supply a message.';
        break;
    }
} while(false);
0. Nó hoạt động bất cứ nơi nào bạn đặt nó thực sự.

Sử dụng nó với
do
{
    $subjectText = trim(filter_input(INPUT_POST, 'subject'));
    if(!$subjectText)
    {
        $smallInfo = 'Please give a subject.';
        break;
    }

    $messageText = trim(filter_input(INPUT_POST, 'message'));
    if(!$messageText)
    {
        $smallInfo = 'Please supply a message.';
        break;
    }
} while(false);
1

Kết quả

do {
  
  if( !process_x() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  
  if( !process_y() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  
  if( !process_z() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  /* SUCCESS */
  
} while (0);
0

Nó cũng hoạt động với

try{
  if( process_x() && process_y() && process_z() ) {
    // all processes successful
    // do something
  } else {
    //one of the processes failed
    clean_all_processes();
  }
}catch(Exception ex){
  // one of the processes raised an exception
  clean_all_processes();
}
9 và
do
{
    $subjectText = trim(filter_input(INPUT_POST, 'subject'));
    if(!$subjectText)
    {
        $smallInfo = 'Please give a subject.';
        break;
    }

    $messageText = trim(filter_input(INPUT_POST, 'message'));
    if(!$messageText)
    {
        $smallInfo = 'Please supply a message.';
        break;
    }
} while(false);
0. Nó hoạt động bất cứ nơi nào bạn đặt nó thực sự.Oct 31, 2017 at 3:55

Hướng dẫn php break out of if - php thoát ra khỏi nếu

1

do {
  
  if( !process_x() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  
  if( !process_y() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  
  if( !process_z() )
    { clean_all_processes();  break; }
  
  /* do a lot of other things */
  /* SUCCESS */
  
} while (0);
1

Sử dụng nó với

do
{
    $subjectText = trim(filter_input(INPUT_POST, 'subject'));
    if(!$subjectText)
    {
        $smallInfo = 'Please give a subject.';
        break;
    }

    $messageText = trim(filter_input(INPUT_POST, 'message'));
    if(!$messageText)
    {
        $smallInfo = 'Please supply a message.';
        break;
    }
} while(false);
1Sep 19, 2011 at 10:16

Trong một trường hợp bình thườngShanon

if(process_x()) {

    /* do a lot of other things */

    if(process_y()) {

         /* do a lot of other things */

         if(process_z()) {

              /* do a lot of other things */
              /* SUCCESS */

         }
         else {

              clean_all_processes();

         }

    }
    else {

         clean_all_processes();

    }

}
else {

    clean_all_processes();

}
91 silver badge3 bronze badges

Bạn có thể thoát khỏi IF Statement PHP không?

Từ khóa phá vỡ kết thúc việc thực hiện dòng điện cho, foreach, trong khi hoặc thực hiện trong khi các vòng lặp.Khi ngắt từ khóa được thực thi bên trong một vòng lặp, điều khiển được tự động chuyển sang câu lệnh đầu tiên bên ngoài vòng lặp.Một sự phá vỡ thường được liên kết với câu lệnh if.A break is usually associated with the if statement.

Làm thế nào để bạn nhảy ra khỏi tình trạng nếu điều kiện?

Bạn không thể thoát ra khỏi một khối.Phải có một lý do tại sao bạn muốn thoát ra khỏi đó nếu không bạn chỉ cần loại bỏ Echo Two và được thực hiện với nó.Đó là trường hợp, chỉ cần đặt phần còn lại của mã trong một điều kiện khác để nó chỉ thực thi khi bạn muốn.. There has to be a reason why you'd want to break out there otherwise you'd just remove echo two and be done with it. That being the case, just enclose the rest of the code in another condition so that it only executes when you want it to.

Làm thế nào để bạn dừng một vòng lặp khi một điều kiện được đáp ứng PHP?

Sử dụng từ khóa Break: Từ khóa Break được sử dụng để chấm dứt ngay vòng lặp và điều khiển chương trình tiếp tục tại câu lệnh tiếp theo theo vòng lặp.Để chấm dứt điều khiển từ bất kỳ vòng lặp nào, chúng tôi cần sử dụng từ khóa Break.: The break keyword is used to immediately terminate the loop and the program control resumes at the next statement following the loop. To terminate the control from any loop we need to use break keyword.

Câu nói PHP nào được sử dụng để nhảy ra khỏi vòng lặp?

Tuyên bố phá vỡ cũng có thể được sử dụng để nhảy ra khỏi một vòng lặp.break statement can also be used to jump out of a loop.