0

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

7 Answers7

5

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

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
1

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.

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
1

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

Forien
  • 2,712
  • 2
  • 13
  • 30
0

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.

André Ferraz
  • 1,511
  • 11
  • 29
0

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;
        }
    }
Mangesh Sathe
  • 1,987
  • 4
  • 21
  • 40
0

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

Bugfixer
  • 2,547
  • 2
  • 26
  • 42
0

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

lastlink
  • 1,505
  • 2
  • 19
  • 29