PHP 错误信息“注意:使用未定义的常量"是什么意思?意思是?

2022-01-23 00:00:00 undefined constants php

PHP 正在日志中写入此错误:注意:使用未定义的常量".

PHP is writing this error in the logs: "Notice: Use of undefined constant".

日志错误:

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]);

这是什么意思,为什么我会看到它?

What does it mean and why am I seeing it?

推荐答案

你应该引用你的数组键:

You should quote your array keys:

$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']);

按原样,它正在寻找名为 departmentnameemailmessage 等的常量.当它没有找到这样的常量时,PHP(奇怪地)将其解释为字符串('department' 等).显然,如果您稍后确实定义了这样一个常量,这很容易破坏(尽管使用小写常量是不好的风格).

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). Obviously, this can easily break if you do defined such a constant later (though it's bad style to have lower-case constants).

相关文章