SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: including files

  1. #1
    SitePoint Guru
    Join Date
    Apr 2001
    Location
    BC, Canada
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    including files

    Quick question here..

    lets say I have 3 files
    main.php
    a.inc
    b.inc

    a.inc
    declares a variable
    '$a' with the value of 'blah'

    b.inc echo's it
    'echo "$a";'

    main.php has
    include a.inc;
    include b.inc;

    will the script output 'blah' or '' (nuthin)
    Because it seems to me that each file is compiled before it is included.. and I dont think it carries certain vars...

    also.. lets say I connected to a database on a.inc
    then on b.inc I do a query to it.. will it work?

  2. #2
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    An analogy would be to say that include() is like taking the contents of the included file and copy-and-pasting them into the file right where the call to include is:

    eg:
    PHP Code:
    a.php
    ----
    <?php
    $a 
    2;
    ?>

    b.php
    ---
    <?php
    $foo 
    += $a;

    main.php
    -------
    <?
    pgp
    $foo 
    1;
    include(
    "a.inc");
    include(
    "b.inc");
    echo 
    $foo;
    ?>
    This is the same as having writen
    PHP Code:
    $foo 1;
    $a 2;
    $foo += $a;
    echo 
    $foo
    You can see that the order in which files are included is important then. Also, don't forget that with include() the include is only made if the include() statement is executed according to the logic of the program. eg:
    PHP Code:
    main.php
    -------
    <?php
    $foo 
    false;

    if (
    $foo) {
       include(
    "a.inc");
    }
    ?>
    According to the logic of the code above the file "a.inc" will never get included.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •