Forum Moderators: coopster

Message Too Old, No Replies

PHP "logic" question

         

createErrorMsg

9:19 pm on Nov 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm going to try not to convolute this too much, but, of course, if I were really capable of unsnarling it myself I probably wouldn't need to post...

My question relates to including files and where it's safe and not safe to assume you have access to the functions and/or variables in those files.

Say I have a page ...

  • index.php which includes a header.php file.
  • header.php includes category_select.php
  • a form generated in category_select.php submits to form_process.php
  • form_process.php includes a functions.php, one function of which produces an output preview form.
  • Submit of the preview form goes back to form_process.php, which then goes to a different function in functions.php that uploads the form data to mySQL and generates an 'upload successful' message.

    Here's the question: if I want the content of header.php to be available on all the generated pages that come through form_process.php (first the generated preview form, then the generated 'upload successful' message), do I also need to include header.php in form_process.php? Or is the include in the original index.php file enough? Obviously, I can figure this out myself by simply not including it and seeing what happens (my plan once I get back to my dev computer).

    I guess my question is actually more general than specific... what are the limits of included files and their contents? At what point to do you lose access to them? Is an included file available only in the file it's included in? Is it available in other files included in that file (so if a.php includes b.php and c.php, are the contents of b.php available to c.php)? Is it available in included included included files (if d.php includes e.php, which includes f.php, can functions in f.php access data in d.php)?

    For some reason, I just can't get my head around the logic here. Any basic run down of the limits of includes would be really helpful and much appreciated. I'm starting to feel like I've gone through the looking glass here...

    Thanks,
    cEM

  • jamie

    9:49 pm on Nov 22, 2004 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    cEM,

    you could test this easily at home by making several small files with variables in it and including them one after another

    e.g.

    a.php is
    <?php
    $var1 = car;
    ?>
    b.php is
    <?php
    $var1 = boat;
    ?>

    and including both of these in c.php
    <?php
    include(a.php);
    include(b.php);
    echo $var1;
    ?>
    etc, etc...

    try to imagine all the code in the included file replacing your include function() - they you'll realise that as long as you are on the same original page, all the data from the includes is available. clear as mud? :-)

    good luck

    IanKelley

    10:09 pm on Nov 22, 2004 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    You may want to use require() instead of inclue().

    Any code brought in using require() is actually added to the program. So anything you require() in index.php becomes part of index.php for all intents and purposes. It would then be available to any other code in the program including code brought in by other require() statements.

    require() is generally always better for things like headers, function files and config files. include() is useful if you want to load an external file conditionally or treat an external file like a function that can return a value.

    createErrorMsg

    3:37 am on Nov 23, 2004 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Ian, once again, thanks for your insight. I ran through the 'stream' of my pages, testing the limits of where I could and could not afford to remove includes. Then I followed your advice and changed most of them to require().

    There were a few instances where I had to leave an include in what seemed like and odd place (like in the midst of the function that generates the 'successful post' message, where without an explicit include() the header remained missing. In this case, I left it as an include(), rather than require().

    Do you think it matters in that context? (The content of the included file in this case is just a variable holding the html markup for the header.)

    IanKelley

    8:01 am on Nov 23, 2004 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    Actually that doesn't make sense based on how require() and include() are supposed to work.

    require() would include the code even if the function that contains it was never called, whereas include() would include the code only if the function was called.

    Nothing wrong with leaving it as include if it works for you though.

    mincklerstraat

    4:32 pm on Nov 23, 2004 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Ian's right here that it's often a good idea to use
    require
    instead of
    include
    - but I'll jump in here by noting how the PHP manual describes the difference between
    include
    and
    require
    :

    require() and include() are identical in every way except how they handle failure.

    - [php.net...]


    In general, if a function is required or included within a function, and nowhere else, it won't get required or included unless that function is actually called.

    Using

    require
    will actually halt execution if a parse error is encountered in the file required, if the file can't be found, or other biggie errors are encountered in that file. This is good if you're running a sensitive script that absolutely must have a certain functionality for safe execution. I'll typically include all files while programming, to make debugging easier, and change crucial ones to
    require
    when I'm done writing and debugging.

    IanKelley

    6:59 am on Nov 24, 2004 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    Thanks for the update... Looks like they changed it not long ago, a lot of the mirrored PHP manuals still have require and include behaving differently.

    Of course that's probably a good thing since a lot of servers are still running the second newest version of PHP 4.