【问题标题】:Define CONSTANT not working, why?定义 CONSTANT 不起作用,为什么?
【发布时间】:2015-08-27 11:03:26
【问题描述】:

我已经在 config.php 上定义了:

define('ROOT', __DIR__);

在 index.php 上:

require_once (ROOT.'/header.php');

结果:

注意:使用未定义的常量 ROOT - 假定为“ROOT”

为什么 ROOT 常量不起作用?我正在学习,有人可以解释一下吗?

编辑

如果我说:

require_once (__DIR__.'/header.php');

它有效。谁能解释一下为什么?

【问题讨论】:

  • 您是否曾经在 index.php 中包含您的 config.php?如果没有,那么 ROOT 将不会在 index.php 中定义
  • require_once "config.php"了吗?
  • 我的 header.php 包含 functions.php / config.php 包含在 functions.php 中。 /Index.php 包含 header.php。也许是命令?我应该把它放在另一个地方吗?
  • 你最好在 index.php 的顶部定义 ROOT;因为首先调用的是 index.php,而 CONSTANT 仅在定义后才可用,然后在您随后包含的所有文件中都可用
  • 只有在包含/需要定义的文件之后才会加载常量,因此您甚至试图在蛋糕烘烤之前吃掉它。

标签: php constants require-once


【解决方案1】:

它不起作用,因为 ROOT 是在 'header.php' 中定义的。

需要在调用header.php之前定义。

感谢您的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-25
    • 2011-06-12
    • 1970-01-01
    • 1970-01-01
    • 2018-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多