PHP 包含不工作?未包含功能

发布于 2024-11-03 03:19:19 字数 509 浏览 5 评论 0原文

以下是该情况的完整背景:

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

首先,我注意到我的包含内容没有被包含在内,但我通过配置我的 php.ini 更改了它。

但是现在,当我尝试包含具有函数的文件时,它无法识别该函数。

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

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

和一个包含该文件的名为 index.php 的文件

<?php include("functions.php"); doit();?>

,我收到此错误消息

Fatal error: Call to undefined function doit() in index.php on line 4

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

趁年轻赶紧闹 2024-11-10 03:19:19

有时,当前目录不是您期望的目录,例如当您包含包含文件中的文件时。

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

<?php
    include($_SERVER['DOCUMENT_ROOT']."/includes/functions.php");
    doit();
?>

如果您的包含目录位于文档根目录之上,则可以使用 < code>.. 仍然从根引用。

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.

ζ澈沫 2024-11-10 03:19:19

因此,如果有人因为遇到同样的问题而偶然发现这个论坛,请让我解释一下出了什么问题以及为什么出了问题。

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

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

活雷疯 2024-11-10 03:19:19

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

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

空袭的梦i 2024-11-10 03:19:19

我也遇到过这个问题。

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

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

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

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

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.

靑春怀旧 2024-11-10 03:19:19

对我来说,问题是由于包含文件中的函数名称与初始文件中的函数名称相同。

我将所有函数名称设置为唯一,不再有问题。

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.

爱她像谁 2024-11-10 03:19:19

我认为比最受支持的帖子中显示的方法稍微好一点的方法是不指向根文件夹,而是使用相对路径。

因此,有时这可能会指向意想不到的路径:

include('/../post-excerpt.php');

您可以这样做:

include(__DIR__.'/../post-excerpt.php');

I think a slightly better approach than the one shown in the most upvoted post is not to point to the root folder, but to use relative paths.

So as this can sometimes can point to unexpected path:

include('/../post-excerpt.php');

you can do instead:

include(__DIR__.'/../post-excerpt.php');
夜光 2024-11-10 03:19:19

我使用了错误的斜杠

I was using the wrong slash ???? (no error message, nothing...)

Wrong (might work on Windows, but not the average hosting)

include 'folder\file.php';

Correct

include 'folder/file.php';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文