PHP 文件包含问题:文件未按预期执行

PHP File Inclusion Issue: File not Executing as Expected

提问人:Bitshost 提问时间:11/13/2023 最后编辑:CBroeBitshost 更新时间:11/13/2023 访问量:42

问:

我遇到了一个问题,即特定的 PHP 文件(custom_functions.php)未在我的项目中执行,从而导致意外行为。

问题背景 文件结构:

project/
│
├── index.php
├── includes/
│   ├── custom_functions.php

以下是 index.php 文件的简化版本:

<?php

    // Trying to include custom functions file
    include 'includes/custom_functions.php';

    // Using a function from the custom functions file
    $result = customFunction();
    echo "<p>Result: $result</p>";
?>

和custom_functions.php文件:

    <?php
    function customFunction() {
    return "This function should return a customized value.";
    }
    ?>

尝试将 custom_functions.php 包含在 index.php 中时会出现此问题。尽管有 include 语句,但 customFunction() 未按预期被识别或执行。未显示任何错误,但该函数似乎在 index.php 中不可用。

我尝试了几种解决方案:

已检查文件路径:已验证文件路径是否准确,以及custom_functions.php文件是否在正确的目录中。

错误报告:在两个文件中设置error_reporting(E_ALL)和ini_set('display_errors', 1),但未显示任何错误。

直接函数调用:当直接在 custom_functions.php 中调用 customFunction() 时,它工作正常,表明文件正在被包含。

请求协助 我将非常感谢关于为什么在 index.php 中无法访问所包含文件的函数的任何见解或建议。有没有人遇到过类似的问题,或者可能知道是什么原因导致了这种意外行为?

提前感谢您的任何帮助!

php 函数 执行 包含

评论


答: 暂无答案