Results 1 to 18 of 18

Thread: [Resolved] Include/Require Problem

  1. #1

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    [Resolved] Include/Require Problem

    This problem has really bothered me for a long time, but I've just tried to get around it. Now it's getting to be annoying, so I'm hoping there's a solution.

    I'm having a problem including/requiring a file within a function, and still having variables it created be defined through a few more function calls. I know that probably doesn't make sense, so I trimmed down my code to the bare-minimum for an example.

    index.php is the main page, which calls a display.php (for displaying news in my program). display.php calls config.php, which simply loads configuration variables into an array.

    Code:
    index.php
    <?php
        // There would normally be some type of if structure
        // here, that's why a function is needed:
    
        main();
    
        function main() {
            require 'display.php';
    
            execute();
        }
    ?>
    
    display.php
    <?php
        require 'config.php';
    
        function execute() {
            global $CONFIG;
    
            echo 'The var: ' . $CONFIG['name'];
        }
    ?>
    
    config.php
    <?php
        // this usually loads values form a database
        $CONFIG['name'] = 'value';
    ?>
    Now, the execute() function of display.php is supposed to output a variable value, but no dice.

    My question to you is, what can I do to make this work, besides fudging around in the index.php file (since I want my users to be able to call it however they need to)?
    Last edited by The Hobo; Feb 25th, 2004 at 11:58 AM.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  2. #2

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Hmm, I changed the code in display.php to this:

    Code:
    display.php
    <?php
        global $CONFIG;
        $CONFIG = require('config.php');
    
        function execute() {
            global $CONFIG;
    
            echo 'The var: ' . $CONFIG['name'] . '<br />';
        }
    ?>
    And it started working. Can anybody explain why?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Because you require display.php from inside the main function. Put the require outside main and the original code should work.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    But I don't want it outside of the main...

    If I have:

    Code:
    function one() {
    
    }
    
    function two() {
    
    }
    
    function three() {
    
    }
    
    
    function four() {
    
    }
    
    
    function five() {
    
    }
    And display.php is only required for three(), I don't want it to have to be there every time, if uneeded.

    Plus, this code is setup by the users of my program, so I don't where they'll place this code.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  5. #5

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I figured since everything happened inside of main, that the variables would be available to every function called by main. I guess I was wrong.

    It's working now, and I guess that's what matters.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The simple problem is that if you include the file from within the function, the "global" variables in that file aren't global anymore. Inclusion is no more than literal inclusion of the other file:
    Code:
    //blabla
    include("some/other.php");
    // foo bar
    simply becomes:
    Code:
    //blabla
    ?>contents of some/other.php<?php
    // foo bar
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I have no idea what you just said.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  8. #8

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    So if I do:

    Code:
    function func1() {
        include 'file.php';
    
        // here
    }
    Variables created in file.php wont be available at the //here point? I know they will be, since I've seen it happen. So if they are available there, and I do:

    Code:
    function func1() {
        include 'file.php';
    
        func2(); // call 2nd function within 1st function
    }
    
    function func2() {
        global $variableCreated;
    
        // here
    }
    If those variables exist, the func2 can't access them? Even though they exist in function?

    Does that mean that global only looks for globals in the global scope (I don't know if that made sense).

    IE:

    Code:
    <?php
        $var1 = 0;
        $var2 = 0;
        $var3 = 0;
    
        function func1() {
            func2();
        }
    
        function func2() {
            global $var4;
        }
    Does global look for $var4 where the other variables are declared only?

    I just figured it would look up the scope by checking func1, then in the global section.

    I know you said this is a simple problem, but I'm still confused...unless what I said above is right...then I understand it.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  9. #9
    Addicted Member
    Join Date
    Sep 2002
    Location
    Durham, NC, US
    Posts
    218
    I'm just messing with PHP, but from my experience with the paradigms of programming, Perl, and ASP:

    A PHP file is not a function unto itself, and a function doesn't see non-global variables outside of itself (it's scope).

    When you did this:
    Code:
    <?php
        require 'config.php';
    
        function execute() {
            global $CONFIG;
    
            echo 'The var: ' . $CONFIG['name'];
        }
    ?>
    
    <?php
        // this usually loads values form a database
        $CONFIG['name'] = 'value';
    ?>
    The execute() doesn't see $CONFIG and is declaring it, anew, as a global.

    When you do this:
    Code:
    <?php
        global $CONFIG;
        $CONFIG = require('config.php');
    
        function execute() {
            global $CONFIG;
    
            echo 'The var: ' . $CONFIG['name'] . '<br />';
        }
    ?>
    $CONFIG is being declared outside execute() as a global, and as such is seen by the function (provided it is called after the variable is declared).

    I could be wrong, but I don't think PHP is that far from every other programming language I've worked with.
    Travis, Kung Foo Journeyman

    Web Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.5 Guide and Reference
    Perl: Documentation, Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    OSS: Mozilla, MySQL (Manual)

  10. #10

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Interesting, but I'm not sure if that's right. I tried this:

    Code:
    test.php
    <?php
    
        require 'config.php';
    
        example();
    
        function example() {
            global $CONFIG;
    
            echo $CONFIG['name'];
        }
    
    ?>
    And it worked, but I think it illustrates what you said above about not working. If example() didn't see $CONFIG and created its on variable, then $CONFIG['name'] wouldn't exist.

    Unless I missed what you were saying. Is that the same or something else?

    I should also note that these two both work in my example:

    Code:
    <?php
        global $CONFIG;
        $CONFIG = require('config.php');
    
        function execute() {
            global $CONFIG;
    
            echo 'The var: ' . $CONFIG['name'] . '<br />';
        }
    ?>
    
    <?php
        global $CONFIG;
        require 'config.php';
    
        function execute() {
            global $CONFIG;
    
            echo 'The var: ' . $CONFIG['name'] . '<br />';
        }
    ?>
    My evil laugh has a squeak in it.

    kristopherwilson.com

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    To explain my last post, just take the three files in your first post. Becuase include() and require() just literally include the files, the PHP parser sees this:
    Code:
    <?php
        // There would normally be some type of if structure
        // here, that's why a function is needed:
    
        main();
    
        function main() {
            ?><?php
        ?><?php
        // this usually loads values form a database
        $CONFIG['name'] = 'value';
    ?><?php
    
        function execute() {
            global $CONFIG;
    
            echo 'The var: ' . $CONFIG['name'];
        }
    ?>
    
    <?php
    
            execute();
        }
    ?>
    Ok, clear this up a little by removing adjacent ?><?php pairs:
    code]<?php
    // There would normally be some type of if structure
    // here, that's why a function is needed:

    main();

    function main() {
    // this usually loads values form a database
    $CONFIG['name'] = 'value';

    function execute() {
    global $CONFIG;

    echo 'The var: ' . $CONFIG['name'];
    }

    execute();
    }
    ?>[/code]
    global indeed only uses variables that really are globals, it doesn't go up in scope.
    So $CONFIG as it comes from config.php is a local variable of function main. In execute, you say that with $CONFIG you want to access the global variable CONFIG. Which is undefined.

    This is why I said that it's better to only include files at the global level, it prevents such bugs.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  12. #12

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by CornedBee
    This is why I said that it's better to only include files at the global level, it prevents such bugs.
    I understand that. But as I said, I can't.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You just said that you don't want to.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  14. #14

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    This issue is resolved. Thanks for your time.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  15. #15
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    I know this is resoved but all you had to do was declare it as (super) global

    $GLOBALS['config'] = $config['name'];

    than every script and function can use it.
    then this would have worked
    Code:
    <?php
    $GLOBALS['config'] = $config['name'];
        require 'config.php';
    
        example();
    
        function example() {
           
            echo $config;
        }
    
    ?>

  16. #16

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Does that clear everytime the script finishes executing?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  17. #17
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    it clears just like any other variable. or until it gets set again.

  18. #18

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Interesting. Thanks for that information.
    My evil laugh has a squeak in it.

    kristopherwilson.com

Posting Permissions

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



Click Here to Expand Forum to Full Width