Break current script, but run next

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sandman

    Break current script, but run next

    Ok, so I have PHP set upp to ato_prepend and auto_append files to every script
    I run. So if I someone surfs to /index.php, these scripts run:

    init.php -> set up DB connections and stuff. Buffers output
    index.php -> The current page with it's layout, output is buffered.
    postprocess.php -> Fetches buffer, applies layout to page.


    Now, sometimes I want to break the execution of index.php (or whatever page
    that is in the middle). For example, when someone submits a form with
    insufficient data, I want to quit executing code and just put up an alert. But
    if I use "die;", then PHP will stop executing all code, it won't just stop
    executing the code in index.php.

    Is there a way to tell PHP to stop executing code in the current script, but
    continue with the next? I would guess that the same could apply to include()'d
    files, where I want the execution of the included file to stop, but the page
    that included the file shouldn't stop executing. I.e.:

    <?
    print "Hello ";
    include("world. php");
    print "!";
    ?>

    And "world.php" would in this case just output "World", but by using some
    method, I could just break the execution of world.php from within world.php
    (without using large IF blocks) and the result would then be "Hello !" from the
    page, and not "Hello " which happens if I use "die;" in world.php.

    Any suggestions?

    --
    Sandman[.net]
  • Pieter Nobes

    #2
    Re: Break current script, but run next

    Sandman wrote:[color=blue]
    > Any suggestions?[/color]

    Perhaps you could use something like:

    <?php
    while (true) {
    // ...
    if ( NOT_ALL_FIELS_C ORRECT ) {
    $message = ERRORMSG;
    break;
    }
    // Rest of code
    break; // Don't forget this or you're thrown in an infinite loop
    }
    ?>

    --
    Pieter Nobels

    Comment

    • Sandman

      #3
      Re: Break current script, but run next

      In article <jH_Uc.218808$j F5.10775687@pho bos.telenet-ops.be>,
      Pieter Nobes <pieterRE@MOVE. opengate.be> wrote:
      [color=blue]
      > Sandman wrote:[color=green]
      > > Any suggestions?[/color]
      >
      > Perhaps you could use something like:
      >
      > <?php
      > while (true) {
      > // ...
      > if ( NOT_ALL_FIELS_C ORRECT ) {
      > $message = ERRORMSG;
      > break;
      > }
      > // Rest of code
      > break; // Don't forget this or you're thrown in an infinite loop
      > }
      > ?>[/color]

      I don't get it. How do I apply this to my files? In which file do I add this? I
      would very much prefer not to have index.php (in this case) be something like:


      if (condition){
      (alert of error and break script
      } else {
      huge block of application, several K's big that would ordinarely be executed
      }

      --
      Sandman[.net]

      Comment

      • Pieter Nobes

        #4
        Re: Break current script, but run next

        Sandman wrote:[color=blue]
        > In article <jH_Uc.218808$j F5.10775687@pho bos.telenet-ops.be>,
        > Pieter Nobes <pieterRE@MOVE. opengate.be> wrote:
        >
        >[color=green]
        >>Sandman wrote:
        >>[color=darkred]
        >>>Any suggestions?[/color]
        >>
        >>Perhaps you could use something like:
        >> ...[/color]
        >
        >
        > I don't get it. How do I apply this to my files? In which file do I add this? I
        > would very much prefer not to have index.php (in this case) be something like:[/color]
        You use it in all your pages. You start a while loop (just an example,
        first possible way I could think of), and when something goes wrong, you
        /break out/ out the wile loop, and the other script (postproccess.p hp)
        will be executed (since that code isn't in the while loop). Process:

        load page
        include init.php
        execute init.php
        go into while loop
        check wether $_GET['name'] is empty
        $_GET['name'] is empty, break out of while loop[color=blue][color=green][color=darkred]
        >>>> the rest of the code in the while loop isn't executed, since we[/color][/color][/color]
        broke out
        include postprocess.php
        execute postprocess.php
        [color=blue]
        > if (condition){
        > (alert of error and break script
        > } else {
        > huge block of application, several K's big that would ordinarely be executed
        > }[/color]

        Why don't you want this? You *have* to say *somewhere* what the
        interpreter has to in which conditions. This seems like a very good and
        fast solution (beter than the while loop ;-) )

        --
        Pieter Nobels

        Comment

        • Sandman

          #5
          Re: Break current script, but run next

          In article <ES%Uc.218869$_ I5.10806365@pho bos.telenet-ops.be>,
          Pieter Nobes <pieterRE@MOVE. opengate.be> wrote:
          [color=blue]
          > Sandman wrote:[color=green]
          > > In article <jH_Uc.218808$j F5.10775687@pho bos.telenet-ops.be>,
          > > Pieter Nobes <pieterRE@MOVE. opengate.be> wrote:
          > >
          > >[color=darkred]
          > >>Sandman wrote:
          > >>
          > >>>Any suggestions?
          > >>
          > >>Perhaps you could use something like:
          > >> ...[/color]
          > >
          > >
          > > I don't get it. How do I apply this to my files? In which file do I add
          > > this? I
          > > would very much prefer not to have index.php (in this case) be something
          > > like:[/color]
          > You use it in all your pages. You start a while loop (just an example,
          > first possible way I could think of), and when something goes wrong, you
          > /break out/ out the wile loop, and the other script (postproccess.p hp)
          > will be executed (since that code isn't in the while loop). Process:
          >
          > load page
          > include init.php
          > execute init.php
          > go into while loop
          > check wether $_GET['name'] is empty
          > $_GET['name'] is empty, break out of while loop[color=green][color=darkred]
          > >>>> the rest of the code in the while loop isn't executed, since we[/color][/color]
          > broke out
          > include postprocess.php
          > execute postprocess.php
          >[color=green]
          > > if (condition){
          > > (alert of error and break script
          > > } else {
          > > huge block of application, several K's big that would ordinarely be
          > > executed
          > > }[/color]
          >
          > Why don't you want this? You *have* to say *somewhere* what the
          > interpreter has to in which conditions. This seems like a very good and
          > fast solution (beter than the while loop ;-) )[/color]

          Ok, before I set up and start trying this, are you saying that I could initiate
          the loop in init.php and end it in postprocess.php ? Like this:

          init.php
          print "Hello";
          while (true){

          index.php
          if (!$_GET["name"]) break
          print "Index";

          postprocess.php
          break;
          }
          print "World!";


          So if the IF in index.php fails, will it only print "HelloWorld !"? What if I
          have a loop in index.php and want to break from that? That is, if you can even
          set a loop to span several scripts...

          I am using the apache option php_append_file and php_prepend_fil e to include
          init.php and postprocess.php to all scripts.

          --
          Sandman[.net]

          Comment

          • Pieter Nobes

            #6
            Re: Break current script, but run next

            Sandman wrote:[color=blue]
            > So if the IF in index.php fails, will it only print "HelloWorld !"? What if I
            > have a loop in index.php and want to break from that? That is, if you can even
            > set a loop to span several scripts...[/color]

            No, that's not possible. You'd have to do it in every script. I think
            this solution is better because if you'd use if/else you'd have almost
            *no* space left because of indenting... i.e.:

            <?php
            if (empty($var1)) break;
            else {
            // do some things
            if ($connection == false) break;
            else {
            // do some things
            if ($numrows = 0) break;
            else {
            // ...
            }
            }
            }
            ?>

            which would, in the end, become *VERY* unclear.

            --
            Pieter Nobels

            Comment

            • Jonathan Epps

              #7
              Re: Break current script, but run next

              Pieter Nobes wrote:[color=blue]
              > Sandman wrote:
              >[color=green]
              >> Any suggestions?[/color]
              >
              >
              > Perhaps you could use something like:
              >
              > <?php
              > while (true) {
              > // ...
              > if ( NOT_ALL_FIELS_C ORRECT ) {
              > $message = ERRORMSG;
              > break;
              > }
              > // Rest of code
              > break; // Don't forget this or you're thrown in an infinite loop
              > }
              > ?>
              >[/color]

              The manual available at php.net uses a do-while loop to do the same
              thing, except that with a do-while loop, you don't have to worry about
              infinite loops if you use do { // ... } while(0);

              <?php
              do {
              // ...
              if ( BOZO_CANNOT_FIL L_OUT_FORMS_COR RECTLY ) {
              $message = ERRORMSG;
              break;
              }
              // ...
              } while (0);
              ?>

              Here's the part of the php.net manual I'm talking about:

              Comment

              Working...