Returning from an included script while inside a function ?

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

    Returning from an included script while inside a function ?

    I have a system where include files puts generated HTML into a variable
    ( $output ).

    In one of the pages I have a function that executes SQL queries and
    return nice arrays.

    The command to abort an included script is 'return' - but not if you're
    within a function. Then 'return' simply aborts that function.

    If the query should fail for some reason, I'd like to overwrite $output
    with a nice err. msg. and then abort execution of the included script
    and return to the main script. But can that be done from within a
    function ?

    TIA
    ------

    Aceb.

  • Ian Collins

    #2
    Re: Returning from an included script while inside a function ?

    joe wrote:[color=blue]
    > I have a system where include files puts generated HTML into a variable
    > ( $output ).
    >
    > In one of the pages I have a function that executes SQL queries and
    > return nice arrays.
    >
    > The command to abort an included script is 'return' - but not if you're
    > within a function. Then 'return' simply aborts that function.
    >
    > If the query should fail for some reason, I'd like to overwrite $output
    > with a nice err. msg. and then abort execution of the included script
    > and return to the main script. But can that be done from within a
    > function ?
    >[/color]
    Throw an exception?

    --
    Ian Collins.

    Comment

    • joe

      #3
      Re: Returning from an included script while inside a function ?

      Even an exception won't let me return to the main script as far as I
      can see

      Comment

      • aschrage16

        #4
        Re: Returning from an included script while inside a function ?

        Maybe I'm not understanding your problem correctly, but can't you just
        make your function output be an array and make one of those values an
        error flag? Then in the main part of the script you could just monitor
        the flag value that is returned and wipe out the output at that point
        (outside the first function) depending on the condition of the flag?

        Comment

        • Al

          #5
          Re: Returning from an included script while inside a function ?

          joe wrote:[color=blue]
          > I have a system where include files puts generated HTML into a variable
          > ( $output ).
          >
          > In one of the pages I have a function that executes SQL queries and
          > return nice arrays.
          >
          > The command to abort an included script is 'return' - but not if you're
          > within a function. Then 'return' simply aborts that function.
          >
          > If the query should fail for some reason, I'd like to overwrite $output
          > with a nice err. msg. and then abort execution of the included script
          > and return to the main script. But can that be done from within a
          > function ?
          >
          > TIA
          > ------
          >
          > Aceb.[/color]

          In my tests this works as expected, but why not make sure by having the
          included file contain a function? Well I know this is awful practise
          and might not actually be viable with what you have but if it's just
          changing one variable do something like this:

          main file:

          <?php

          echo outputstuff();

          function outputstuff() {
          include ("include.php") ;
          $output = includefile_fun ction();

          echo "still running!";
          return $output;
          }

          ?>


          and in include.php:

          <?php

          function includefile_fun ction() {
          $error = false;
          $HTML = "";

          // do normal stuff
          if ($error) return "Error!";

          // do more stuff
          if ($error) return "Error!";

          // whatever

          return $HTML;
          }

          ?>


          That should work, right?



          My test was:

          main file:

          <?php

          echo outputstuff();

          function outputstuff() {
          $output = "this'll be overwritten";
          include ("include.php") ;

          echo "still running!";
          return $output;
          }

          ?>

          and in include.php:

          <?php

          $error = false;
          $HTML = "";

          $HTML = "1";
          if ($error) { $output = "Error!"; return; }

          $HTML = "2";
          if ($error) { $output = "Error!"; return; }

          // whatever
          $output = $HTML;

          ?>

          Comment

          • joe

            #6
            Re: Returning from an included script while inside a function ?

            Yes indeed I could, and that is probably what I'll end up doing.

            It just seems like a shame that return can't exit multiple levels
            (first exit the function - then exit the include file), because it
            would make things simpler. That way I'd only have to write the
            errorcheck one place. Now I have to write it several places.

            So yeah - it's not a showstopper, only a minor nuisance :)

            Comment

            • joe

              #7
              Re: Returning from an included script while inside a function ?

              Thanks for the answer !

              If there is a double quotation of your reply - I apologize. I am using
              google groups interface, and it appears it won't let me use 'broken'
              quotes.
              [color=blue]
              > In my tests this works as expected, but why not make sure by having the
              > included file contain a function?[/color]

              Yep - I already have a function in the included file :)
              [color=blue]
              > Well I know this is awful practise
              > and might not actually be viable with what you have[/color]

              Not sure I agree it's an awful practise. I have often used 'utility'
              include files that included functions that simplified things I had to
              do often.

              Also if you do objects - including is a must, otherwise the
              practicality of seperating business logic from the objects is lost :)

              Your first example would work, but alas it is not viable in my
              situation. I am fixing someone elses work, and it's a mess. Totally
              sequentially written - then we do this - then we do this. The main file
              is index.php and it branches execution to various include files with a
              switch statement. It is in fact the only file that is ever called
              directly in this system. If I alter the main file - I alter
              functionality for a ton of other files, which I'd then have to re-edit
              and bugtest.

              Right now I am fixing the search_inc.php file, in which various
              database calls is scattered all over a mess of 'if(thisorthat) then we
              construct this horrible SQL and add that HTML' code. In order for
              sanity to prevail, I am arranging things nice and neatly into
              functions. I can do this within this include file, but I dare not
              venture into any changes that might rock the rest of the system.

              Another reason is that the databasefunctio n is called from other
              functions - so I'd have to do an error check after all calls (which is
              what I'll end up doing anyway as pr. aschrage16's suggestion) - so
              even if I overwrite $output, new content (like end of tables that are
              no longer there) will be generated, rendering that whole exercise
              obsolete.

              At any rate - the problem is not an obstacle, I just thought that since
              there IS an 'escape' mechanism for an included file (a return statement
              in the global scope), it's odd and a bit annoying that you can't
              invoke that mechanism from inside a function.

              Comment

              Working...