phpincludeundefined

Call to undefined function from another php file


Alright this is what my code looks like

index.php

require_once($WebsiteRoot . "/include/testfile.php");
TestFunction();

/include/testfile.php

function TestFunction()
{
    echo "It Works";
}

And it gives me the error:

Fatal error:
Call to undefined function TestFunction() in /path/index.php on line 49

Any idea what i'm doing wrong? Thanks


Solution

  • Make sure you're including the file you think you are. If your index.php page looks exactly like you've stated, then it won't return anything.

    If you want to link to the same location from anywhere on the site without worrying about relative locations, then at the beginning of the file, put:

    $WebsiteRoot=$_SERVER['DOCUMENT_ROOT'];
    

    And it should work fine, provided your file would be located at http://mywebsite.com/include/testfile.php