How to continue a php script after using exit() / die()

不问归期 提交于 2019-12-25 03:12:33

问题


For example i have the following script.

in this case i want to get the value of X1 and Y1 but the the exit() is not letting me to do so

please help thanks in advance and special thanks to Mark Setchell :P (if he sees this)

image link

 $im = imagecreatefromjpeg("omr.jpg");
for($x=0;$x<100;$x++)   {

    for($y=0;$y<100;$y++)   
                            {

$rgb = imagecolorat($im,$x,$y);
      $r = ($rgb >> 16) & 0xFF;
      $g = ($rgb >> 8) & 0xFF;
      $b = $rgb & 0xFF;
      if($r<128 && $g<128 && $b<128){
         printf("%d,%d: %d,%d,%d\n",$x,$y,$r,$g,$b);
         exit;
                                    }
                            }
                        }

 for($x1=1170;$x1<1270;$x1++){

 for($y1=0;$y1<100;$y1++){

          $rgb = imagecolorat($im,$x1,$y1);

          $r1 = ($rgb >> 16) & 0xFF;
          $g1 = ($rgb >> 8) & 0xFF;
          $b1 = $rgb & 0xFF;
          if($r1<128 && $g1<128 && $b1<128){
          printf("%d,%d: %d,%d,%d\n",$x1,$y1,$r1,$g1,$b1);
          exit;
        }
       }
    }

current output : 30,53: 123,119,118

Desired output : 30,53: 123,119,118

1217,55: 115,114,112


回答1:


You can use break 2; to break from two nested for loops and your next for loop for x1 and y1 will also execute.




回答2:


exit

exit — Output a message and terminate the current script

die

die — Equivalent to exit

Hence, you cannot do that, period. You can however rethink your solution where you don't have to use exit there because you want to continue the execution then exit is in the wrong place.

Now for the solution: You can break out of your loop when your condition is met.

break

break ends execution of the current for, foreach, while, do-while or switch structure.

That way your loop will end and you will be able to move on to your next loop without exit.




回答3:


Uhm, it's not possible.

exit;              // equal to:    die;
exit();            // equal to:    die();
exit('I died');    // equal to:    die('I died');

are language construct designed to kill PHP process. As the Manual says:

Output a message and terminate the current script

Don't use it if you want script to continue.

In loops you can use two costructs you may be interested in:

break; - Immediately ends loop and continues code after the loop
continue; - Immediately skips current loop iteration and goes to the next iteration




回答4:


You shouldn't be using the die() or exit(), you should instead use the break function. Click Here, for more info.

exit() or and die() should be used only if you want to end the script.

exit — Output a message and terminate the current script docs here.

die — Equivalent to exit, docs here.




回答5:


Check http://php.net/manual/en/control-structures.break.php

depending on your scope , you can use break 1 or break 2,

$i = 0;
    while (++$i) {
        switch ($i) {
        case 5:
            echo "At 5<br />\n";
            break 1;  /* Exit only the switch. */
        case 10:
            echo "At 10; quitting<br />\n";
            break 2;  /* Exit the switch and the while. */
        default:
            break;
        }
    }



回答6:


You cannot run a script after die() or exit() until unless you use condition or switch statement.




回答7:


You might want to run a background php process if you wish to continue running scripts after a page has been rendered, unless you're asking to just run more changes to the page when a for loop has been terminated, in this case a break; would be best.

stack overflow php background process



来源:https://stackoverflow.com/questions/28737328/how-to-continue-a-php-script-after-using-exit-die

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!