Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Calling one include's function in another include.

    I have a file like this...

    (test1.php)
    PHP Code:
    <?php
    include("test2.php");
    include(
    "test3.php");
    ?>
    (test2.php)
    PHP Code:
    <?php 
    function site_welcome() {
        echo 
    'Welcome to the site!<br>
    '
    ;
    }
    site_welcome();
    ?>
    (test3.php)
    PHP Code:
    <?php site_welcome(); ?>
    (Result)
    Welcome to the site!

    Fatal error: Call to undefined function: site_welcome() in test3.php on line 1

    (My Question)
    How do I go about calling functions from test2 in test3? Is that even possible?

  • #2
    Regular Coder
    Join Date
    Jan 2006
    Posts
    377
    Thanks
    8
    Thanked 1 Time in 1 Post
    In your case, test3.php does not know what site_welcome() is.

    Gather all your function in a functions.php and include it in every file.

    functions.php:

    PHP Code:
    function site_welcome() {
        echo 
    'Welcome to the site!<br>';

    test3.php:

    PHP Code:
    include("functions.php");
    site_welcome(); 
    Last edited by guvenck; 07-10-2007 at 09:10 PM.

  • #3
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Is there is no other way to make one function active for every include oppose to adding it to each one individually? The reason I ask is because I am currently using sessions where the user is logged in, but the file that says "Welcome, username" is in an include that deviates from the original session. Since that makes it unable to read the user, it always results in a "Welcome, Guest" printout.

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Blocksbox, this should work fine. Includes and Requires insert the included file on the line where the include is located. As long as that is not inside some other function that cannot see a function somewhere else, it will work. I just ran the following test (v.4.4.6) and the function inside t2.php was visible to t3.php.

    PHP Code:
    t1.php
    <?php
    echo "begin t1<br />";
    require(
    "t2.php");
    require(
    "t3.php");
        
    ?>

    t2.php
    <?php
    echo "begin t2<br />";

    function 
    testFunc() {
        echo 
    "inside testFUnc<br />";
    }
        
    ?>

    t3.php
    <?php
    echo "begin t3<br />";

    testFunc();
    ?>

    Result was:
    begin t1
    begin t2
    begin t3
    inside testFUnc


  •  

    Posting Permissions

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