Call to undefined function from another php file
Asked Answered
S

6

5

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

Sizing answered 28/5, 2012 at 16:31 Comment(7)
The only explanation, given the information, is that you're not including the file you think you're including.Ernestineernesto
echo $WebsiteRoot . "/include/testfile.php"; What do you see?Artemisa
If your index.php is hosted on same file structure level as the include folder, you don't need $WebisteRoot variable. I guess the error comes from that.Acetanilide
there are 2 situations:1) the function doesn't exist in your testfile.php 2) the path to the testfile.php isn't right !Teredo
It's probably supposed to be require_once("./include/testfile.php");Nejd
With the given info, try it without the $websiteRoot, and perhaps the first forward slash.Lilith
Try echo $WebsiteRoot . "/include/testfile.php"; just to see if it even is the fileSeften
A
0

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

Ablution answered 28/5, 2012 at 16:35 Comment(4)
Yeah sorry that isn't the problem, the $WebsiteRoot is defined as the website's urlSizing
Actually, if set $WebsiteRoot to $_SERVER['DOCUMENT_ROOT'], it does seem to workSizing
Don't use URLs to include local files! Use file system paths instead. If you fetch the file with a URL the server parses the file instead of giving the code. You're probably getting a blank file through that.Ernestineernesto
Yep, that was the problem, $WebsiteRoot was defined as website.com, that's why it didn't work thanks JuhanaSizing
D
8

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.

Delphinus answered 28/5, 2012 at 17:13 Comment(0)
W
1

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";
Whitson answered 28/5, 2012 at 16:35 Comment(2)
Adding this to the top of index.php I'm getting the same resultSizing
those lines will not fix your problem, but show your error(s), from there you can debug. I have updated the answer a bit for you.Whitson
T
1

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.

Toothed answered 31/5, 2019 at 3:37 Comment(0)
A
0

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

Ablution answered 28/5, 2012 at 16:35 Comment(4)
Yeah sorry that isn't the problem, the $WebsiteRoot is defined as the website's urlSizing
Actually, if set $WebsiteRoot to $_SERVER['DOCUMENT_ROOT'], it does seem to workSizing
Don't use URLs to include local files! Use file system paths instead. If you fetch the file with a URL the server parses the file instead of giving the code. You're probably getting a blank file through that.Ernestineernesto
Yep, that was the problem, $WebsiteRoot was defined as website.com, that's why it didn't work thanks JuhanaSizing
C
0

Try renaming the included file.

I had an included file with the name "system.php". It looked as if the include command was just skipped. Even with the most strict error reporting there was no message and even an echo command in the main body of the included file did not produce output. It had worked ok under PHP 5 but after the upgrade to a 7.2 environment these problems arose. After much effort - I forgot how - I managed to get an error message. It said there was a conflict with a PEAR class with the name "system". Yet my file didn't contain any class, just variables and functions. Anyway, giving the file another name than "system.php" worked for me.

I hope someone else can add a more technical comment on what was going wrong here.

Crifasi answered 21/2, 2020 at 9:15 Comment(0)
C
0

I had the same problem right now. My solution was that closing <?php tag was missing. It worked for 12 years, stopped working now. Probably new versions of PHP or server config. I am out of web game for 10 years, but fixed it with closing tags in all files. I thought it used to be a good practice not to close it in the past :)

So not only openig with:

<?php

But also closing with:

?>

is an important part.

Casement answered 17/6, 2024 at 9:31 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.