PHP 错误消息“使用未定义常量”是什么意思?意思是?
PHP 在日志中写入此错误:“注意:使用未定义的常量”。
日志中的错误:
PHP Notice: Use of undefined constant department - assumed 'department' (line 5)
PHP Notice: Use of undefined constant name - assumed 'name' (line 6)
PHP Notice: Use of undefined constant email - assumed 'email' (line 7)
PHP Notice: Use of undefined constant message - assumed 'message' (line 8)
相关代码行:
$department = mysql_real_escape_string($_POST[department]);
$name = mysql_real_escape_string($_POST[name]);
$email = mysql_real_escape_string($_POST[email]);
$message = mysql_real_escape_string($_POST[message]);
这是什么意思以及为什么我会看到它?
PHP is writing this error in the logs: "Notice: Use of undefined constant".
Error in logs:
PHP Notice: Use of undefined constant department - assumed 'department' (line 5)
PHP Notice: Use of undefined constant name - assumed 'name' (line 6)
PHP Notice: Use of undefined constant email - assumed 'email' (line 7)
PHP Notice: Use of undefined constant message - assumed 'message' (line 8)
Relevant lines of code:
$department = mysql_real_escape_string($_POST[department]);
$name = mysql_real_escape_string($_POST[name]);
$email = mysql_real_escape_string($_POST[email]);
$message = mysql_real_escape_string($_POST[message]);
What does it mean and why am I seeing it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的数组键是字符串。 PHP 中的字符串文字必须加引号:
按原样,它正在查找名为
department
、name
、email
、message
的常量。 code> 等。当它找不到这样的常量时,PHP(奇怪地)将其解释为字符串(“部门”等),但会对此发出警告。显然,如果您稍后定义这样的常量,这很容易被破坏(尽管使用小写常量是不好的风格)。更新:此行为后来在 PHP 8 中得到修复:“如果使用未定义的常量,则会引发错误。” (PHP:语法 - 手册)
Your array keys are strings. String literals must be quoted in PHP:
As is, it was looking for constants called
department
,name
,email
,message
, etc. When it doesn't find such a constant, PHP (bizarrely) interprets it as a string ('department', etc), but warns you about that. Obviously, this can easily break if you do define such a constant later (though it's bad style to have lower-case constants).Update: This behavior was later fixed in PHP 8: "If an undefined constant is used an Error is thrown." (PHP: Syntax - Manual)
该错误消息是由于 PHP 会隐式将未知标记声明为同名常量字符串而导致的。
也就是说,它试图解释这一点(注意缺少的引号):
在 PHP 中,唯一有效的语法是如果之前定义了一个常量
department
。遗憾的是,它并没有在此时因致命错误而死亡,而是发出此通知,并表现得好像已使用相同的名称和值定义了一个常量:您可以通过多种方式获取此错误消息,但它们都有相同的根本原因 - 可以是常量的标记。
字符串缺少引号:
$my_array[bad_key]
这就是您的情况的问题所在,这是因为您的字符串数组键尚未被引用。修复字符串键将修复该错误:
更改:更改
为:
变量缺少美元符号:
var_without_dollar
您可能会看到此错误消息的另一个原因是,如果您在变量,或来自成员的
$this->
。例如,以下任一情况都会导致类似的错误消息:变量名称中的无效字符:
$bad-variable-name
如果您尝试在变量中使用不允许的字符,则可能会导致类似但更微妙的问题。变量名 - 连字符 (
-
) 而不是下划线_
是常见情况。例如,这是可以的,因为 变量名称中允许使用下划线:
但这不是:
它将被解释为与此相同:
引用类常量而不指定类范围
为了引用类常量,您需要使用
::< 指定类范围/code>,如果你错过了这一点,PHP 会认为你正在谈论一个全局
define()
。例如:
使用未在此版本的 PHP 中定义的常量,或者在未安装的扩展中定义的常量
有一些系统定义的常量仅存在于较新版本的 PHP 中,例如
round()
例如PHP_ROUND_HALF_DOWN
存在于 PHP 5.3 或更高版本中。因此,如果您尝试在 PHP 5.2 中使用此功能,请说:
您会收到以下错误消息:
The error message is due to the unfortunate fact that PHP will implicitly declare an unknown token as a constant string of the same name.
That is, it's trying to interpret this (note the missing quote marks):
The only valid way this would be valid syntax in PHP is if there was previously a constant
department
defined. So sadly, rather than dying with a Fatal error at this point, it issues this Notice and acts as though a constant had been defined with the same name and value:There are various ways you can get this error message, but they all have the same root cause - a token that could be a constant.
Strings missing quotes:
$my_array[bad_key]
This is what the problem is in your case, and it's because you've got string array keys that haven't been quoted. Fixing the string keys will fix the bug:
Change:
To:
Variable missing dollar sign:
var_without_dollar
Another reason you might see this error message is if you leave off the
$
from a variable, or$this->
from a member. Eg, either of the following would cause a similar error message:Invalid character in variable name:
$bad-variable-name
A similar but more subtle issue can result if you try to use a disallowed character in a variable name - a hyphen (
-
) instead of an underscore_
would be a common case.For example, this is OK, since underscores are allowed in variable names:
But this isn't:
It'll be interpreted the same as this:
Referring to a class constant without specifying the class scope
In order to refer to a class constant you need to specify the class scope with
::
, if you miss this off PHP will think you're talking about a globaldefine()
.Eg:
Using a constant that's not defined in this version of PHP, or is defined in an extension that's not installed
There are some system-defined constants that only exist in newer versions of PHP, for example the mode option constants for
round()
such asPHP_ROUND_HALF_DOWN
only exist in PHP 5.3 or later.So if you tried to use this feature in PHP 5.2, say:
You'd get this error message: