Php include not working? function not being included

两盒软妹~` 提交于 2020-01-10 08:30:10

问题


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


回答1:


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.




回答2:


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




回答3:


Try require() instead of include. Perhaps include is failing and errors are not being shown.




回答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.




回答5:


For me the problem was due to a function name in the included file having the same name as a function in the initial file.

I made all the function names unique and no longer have the problem.




回答6:


This is weird. I have the following.

include_once('assets\include\page-essentials\functions-init.php'); 
include_once('assets\include\page-essentials\functions-api.php');
include_once('assets\include\php\results.php');
include_once('assets\include\page-essentials\url-availability.php');

//require($_SERVER['DOCUMENT_ROOT']."\FBH-2020\assets\include\php\boatfeaturecode.php");
require("\assets\include\php\boatfeaturecode.php");

The first three includes ALL contain functions and ALL work (that is functions-init.php, functions-api.php and results.php)

The line that requires boatfeaturecode.php does not work, but the commented line with teh $Server['DOCUMENT_ROOT'] works.

I cannot see why this is.

it dosen't matter whether this is an INCLUDE OR REQUIRE command as to whether it works or not.

This is IIS on windows 10. The dicrctory structure is standard C:\inetpub\wwwroot\

The file that I am calling these includes from is in C:\inetpub\wwwroot\FBH-2020\

where the path of the included files is

C:\inetpub\wwwroot\FBH-2020\assets\ --- etc as shown in the first three/four includes that work perfectly.



来源:https://stackoverflow.com/questions/5762017/php-include-not-working-function-not-being-included

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