Call to undefined function from another php file

我们两清 提交于 2019-12-31 00:07:10

问题


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


回答1:


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




回答2:


You haven't included a <?php tag in the included file, so it's just interpreted as plaintext input.

Remember... there's no such thing as a PHP script. There's only files which contain PHP code blocks. Without at least one <?php opening tag, the PHP interpreter will never be invoked and the file's contents will simply be treated as output.




回答3:


try calling another function from testfile.php, if this is'nt working, its something with the include. Add the code:

error_reporting(E_ALL | E_WARNING | E_NOTICE);
ini_set('display_errors', TRUE);

to the top of index.php and refresh the browser to see your errors, try debugging from there.

The problem that i can forsee is that you are using a URL instead of a path, your $websiteRoot variable should contain a path like:

$websiteRoot = "/var/www/html/websiteName";
OR
$websiteRoot = "C://xampp/htdocs/websiteName";

instead of a URL like:

$websiteRoot = "http://www.somesite.com";



回答4:


I had a similar issue. I dug into the PHP in the included file and found an invalid PHP tag. I had <? instead of <?php. PHP 7.2 and earlier forgave that, but PHP 7.3 was throwing that same error you faced.



来源:https://stackoverflow.com/questions/10787796/call-to-undefined-function-from-another-php-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!