![](/img/trans.png)
[英]Use of undefined constant String - assumed 'String' when calling a class name from variable
[英]Use of undefined constant [variable_name] - assumed [variable_.name] error in Php
简介 :
我正在一个非常简单的php项目中,我在其中创建了一个config.php文件,其中包含维护网站所需的所有应用程序/配置变量,例如:[数据库/服务器/用户名/密码等]
现在,当我从配置文件中调用变量时,它将引发错误。
问题的产生。
文件层次结构:
- /..
- lib
: :
: - config.php
: - include.php
:
- index.php
config.php代码
<?php
define('database','dbTest');
?>
include.php的代码
<?php
include('config.php');
echo database; // comment this line when u make index.php
?>
[如果您使用xampp运行include.php,由于dbTest的原因很简单,因为您已经包含config.php,我们很可能会访问数据库变量。
现在的问题是,当我们在另一个文件中包含include.php文件时。
index.php代码
<?php
include('lib/include.php');
echo database;
?>
现在,当我运行index.php时,出现以下错误。
错误
注意:使用未定义的常量数据库-在第3行数据库的C:\\ xampp \\ htdocs .. \\ test \\ lib \\ sam.php中假定为“数据库”
你能指导我如何解决这个问题吗?
将include.php中的include更改为:
include(__DIR__ . '/config.php');
那是因为相对路径是相对于index.php而不是include.php的 。 但是__DIR__
将为您提供包含include.php的文件夹的绝对路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.