Php include not working? function not being included
Solution 1
So if anyone ever stumbles on this forum because they are having the same issue let me explain what and why it went wrong.
If you include a function not in your directory(e.g c:// or file://) but instead include using http. The include can only return what was echoed in the file, but something like a variable or function will not be shown. So always include functions and variables through a directory
Solution 2
Sometimes the current directory isn't what you expect it to be, such as when you include a file from an included file.
I like to use $_SERVER['DOCUMENT_ROOT']
on my includes so that I can always reference them absolutely from the root of my site:
<?php
include($_SERVER['DOCUMENT_ROOT']."/includes/functions.php");
doit();
?>
If your includes directory is above your document root, you can use ..
to still reference from the root.
Solution 3
Try require() instead of include. Perhaps include is failing and errors are not being shown.
Solution 4
I have been got that problem too.
In my case, I find out it may be your "functions.php" file Permission denied.
Please try to "chmod 777 functions.php" in the server.
Let the functions.php can execute in the web server.
Thanks Thatjuan, Becasue when I change to use require(), the server show off the right error message.
Tom
Updated on March 03, 2020Comments
-
Tom about 4 years
Here is the full context of the situation:
I recently got a new Mac, I'm a php developer so I downloaded MAMP and started developing.
First I noticed that my includes were not being included, but I changed that by configuring my
php.ini
.However now, when I try to include a file with a function it does not recognize the function.
For example I have a file named
functions.php
:<?php function doit(){ echo "did it"; } ?>
and a file that includes it called index.php
<?php include("functions.php"); doit();?>
and I get this error message
Fatal error: Call to undefined function doit() in index.php on line 4
-
0x6A75616E almost 13 yearsWhich means functions.php is in a different directory. To test you could use the full path to the file. For example /var/www/site/functions.php
-
Tom almost 13 yearsThe path I use is this localhost:8888/perfectur/perfectur/functions/formfunctions.php
-
Tom almost 13 yearsThe behavior is very strange since if I just tell it to echo text it does however if I include a function it can not see it and gives me and error.
-
Elijan Sejic almost 13 yearslocalhost:8888/perfectur/perfectur/functions/formfunctions.php -- this is not a file path!.
-
0x6A75616E almost 13 yearsYou need to try the path of functions.php within the system and not its url. Do you have console access? If so just find out what directory is the file in and include it using the full path.
-
0x6A75616E almost 13 yearsThe answer you are looking for ultimately is that you need to find out the path of functions.php relative to the file you are including it from. E.g. If it is in a folder called "include", you would use require( "include/functions.php" )
-
Tom almost 13 yearsJaun D thank you so much. You have saved my website and self esteem.
-
Tom almost 13 yearsIt works. Thank you so much. I would hug you if I could wow. Thanks. So stupid of me. I included it correctly and it worked
-
TsukiakariUsagi almost 13 yearsI spent the better part of the last day at work trying to figure out this /exact/ problem. Because of the weirdness with PHP, it was not finding my functions inside any of my included files and I did reference via HTTP, but then I ran across the comment above using $_SERVER['DOCUMENT_ROOT'] and it has made everything work wonderfully (finally!).
-
TsukiakariUsagi almost 13 yearsA million times yes, and thank you! This was driving me up the wall not knowing why my stuff wasn't working.
-
abettermap over 9 yearsHad similar issue with WordPress when using
$get_stylesheet_directory_uri
, so I began referencing withABSPATH
instead.$_SERVER['DOCUMENT_ROOT']
would have been fine, however, if my dev and production environments had the same directory structure.