Hướng dẫn php break if - ngắt php nếu

(Php 4, Php 5, Php 7, Php 8)

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 Kết thúc thực hiện hiện tại
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,
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,
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,
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);
6 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);
7.

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 chấp nhận một đối số số tùy chọn cho biết có bao nhiêu cấu trúc bao quanh lồng nhau sẽ bị phá vỡ. Giá trị mặc định là
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);
9, chỉ có cấu trúc bao quanh ngay lập tức được chia ra.

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();
}
0

Ei Dot Dwaps tại Gmail Dot Com ¶

2 năm trước

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

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();
}
2

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();
}
3

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();
}
4

Đã trả lời ngày 31 tháng 10 năm 2017 lúc 3:55

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

echo "finish";

Đã trả lời ngày 19 tháng 9 năm 2011 lúc 10:16

Shanonshanon

$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";

91 Huy hiệu bạc3 Huy hiệu đồng

Có bất kỳ lệnh nào trong PHP để ngừng thực thi câu lệ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();
}
5 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);
2 hoặc
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 cho ________ 37/________ 49. Ví dụ

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

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();
}
0 và sẽ được chuyển sang
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();
}
1Sep 19, 2011 at 9:29

Tôi cần cái này cho nếuMuhammad Usman

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);
2 Tuyên bố
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();
}
5 ở trên và ngừng thực thi
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();
}
4 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?6 gold badges35 silver badges58 bronze badges

5

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:29

Muhammad Usmanmuhammad Usman

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();

}

12.3k6 Huy hiệu vàng35 Huy hiệu bạc58 Huy hiệu Đồng

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);

Đô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ỡ

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();
}
5, rất nhiều
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();
}
5 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.

Hướng dẫn php break if - ngắt php nếu

Mã mẫu này minh họa rằng trong một số tình huống đã bị phá vỡ

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();
}
5 có thể phù hợp hơn nhiều so với rất nhiều
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();
}
5 được lồng xấu xí.

Mã xấu xí7 gold badges37 silver badges72 bronze badges

Mã đẹp mắtNov 15, 2011 at 1:30

Hướng dẫn php break if - ngắt php nếu

Như @niematojaktomasz nói, việc 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();
}
9 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).AgelessEssence

Adriaan5 gold badges31 silver badges36 bronze badges

10

17.6K7 Huy hiệu vàng37 Huy hiệu bạc72 Huy hiệu đồng

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

Hướng dẫn php break if - ngắt php nếu

4

Agelessessenceaglessessence

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.0955 Huy hiệu vàng31 Huy hiệu bạc36 Huy hiệu đồ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();
}

Đó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

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 bất cứ lúc nào.

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

Hướng dẫn php break if - ngắt php nếu

3

Cách thích hợp để làm điều này: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);

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":Jun 12, 2016 at 8:18

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.Markus Zeller

Đã trả lời ngày 7 tháng 1 năm 2014 lúc 22:182 gold badges30 silver badges33 bronze badges

1

goto:

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.

Gnat

6.210104 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() && 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
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

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


}



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

NHƯNG HÃY NHỚ!

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 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 if - ngắt php 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):

    <?php
    do if ($foo)
    {
      // Do something first...

      // Shall we continue with this block, or exit now?
      if ($abort_if_block) break;

      // Continue doing something...

    } while (false);
    ?>

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 if - ngắt php 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 .________ 63 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);
7!
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);
7 !

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

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);
5.
If the break behaviour you are trying to set is not about unexpected error(s), Exception handling is not the right solution here
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);
5.

Đã 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ạc46 Huy hiệu đồng5 gold badges33 silver badges46 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 if - ngắt php 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 if - ngắt php 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

Chỉ cần di chuyển mã không được thực hiện cho nhánh

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);
6. Tôi thực sự không thấy tại sao bạn muốn làm những gì bạn đang cố gắng làm.

Đã 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

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();
}
5 mà không dừng hoàn toàn việc thực thi (thông qua
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);
8). 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
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();
}
5, 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 if - ngắt php 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 if - ngắt php 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 if - ngắt php 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ị

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


}



My123:
....your code here....
0.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 ở

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


}



My123:
....your code here....
0 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

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


}



My123:
....your code here....
2 và
if(smth) {
   .....
   .....
   .....
   .....
   .....
   goto My123;
   .....
   .....


}



My123:
....your code here....
3.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

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


}



My123:
....your code here....
2 và
if(smth) {
   .....
   .....
   .....
   .....
   .....
   goto My123;
   .....
   .....


}



My123:
....your code here....
3.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

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


}



My123:
....your code here....
2 và
if(smth) {
   .....
   .....
   .....
   .....
   .....
   goto My123;
   .....
   .....


}



My123:
....your code here....
3.Nó hoạt động bất cứ nơi nào bạn đặt nó thực sự.

Sử dụng nó với
if(smth) {
   .....
   .....
   .....
   .....
   .....
   goto My123;
   .....
   .....


}



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

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

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


}



My123:
....your code here....
2 và
if(smth) {
   .....
   .....
   .....
   .....
   .....
   goto My123;
   .....
   .....


}



My123:
....your code here....
3.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 if - ngắt php 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

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


}



My123:
....your code here....
4Sep 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