PHP 包括不工作?不包括功能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5762017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:24:29  来源:igfitidea点击:

Php include not working? function not being included

phpfunctioninclude-path

提问by Tom

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.

我最近买了一台新 Mac,我是一名 php 开发人员,所以我下载了 MAMP 并开始开发。

First I noticed that my includes were not being included, but I changed that by configuring my php.ini.

首先我注意到我的包含没有被包含,但我通过配置我的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:

例如,我有一个名为的文件functions.php

<?php
function doit(){
    echo "did it";
}
?>

and a file that includes it called index.php

以及一个包含它的名为 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

致命错误:在第 4 行的 index.php 中调用未定义的函数 doit()

采纳答案by Tom

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

如果您在目录中包含一个函数(例如 c:// 或 file://),而是使用 http 包含。include 只能返回文件中回显的内容,但不会显示变量或函数之类的内容。所以总是通过目录包含函数和变量

回答by Sean Adkinson

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:

我喜欢$_SERVER['DOCUMENT_ROOT']在我的包含中使用,以便我始终可以从我网站的根目录绝对引用它们:

<?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.

如果您的包含目录在您的文档根目录之上,您..仍然可以使用从根目录引用。

回答by 0x6A75616E

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

尝试 require() 而不是 include。也许包含失败并且没有显示错误。

回答by Andrew Kin Fat Choi

I have been got that problem too.

我也遇到过这个问题。

In my case, I find out it may be your "functions.php" file Permission denied.

就我而言,我发现它可能是您的“functions.php”文件权限被拒绝。

Please try to "chmod 777 functions.php" in the server.

请尝试在服务器中“chmod 777 functions.php”。

Let the functions.php can execute in the web server.

让functions.php可以在web服务器中执行。

Thanks Thatjuan, Becasue when I change to use require(), the server show off the right error message.

谢谢 Thatjuan,因为当我改为使用 require() 时,服务器会显示正确的错误消息。

回答by Paul B

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.

我使所有函数名称都是唯一的,不再有问题。

回答by Andrew Banner

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)

前三个包括所有包含函数和所有工作(即functions-init.php、functions-api.php和results.php)

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

需要boatfeaturecode.php 的行不起作用,但带有$Server['DOCUMENT_ROOT'] 的注释行有效。

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.

无论它是否有效,这是否是 INCLUDE 或 REQUIRE 命令都无关紧要。

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

这是 Windows 10 上的 IIS。目录结构是标准的 C:\inetpub\wwwroot\

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

我调用这些包含的文件位于 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.

C:\inetpub\wwwroot\FBH-2020\assets\ --- 等等,如前三个/四个中所示,工作完美。