PHP 错误消息“注意:使用未定义常量”是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/2941169/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does the PHP error message "Notice: Use of undefined constant" mean?
提问by Nik
PHP is writing this error in the logs: "Notice: Use of undefined constant".
PHP 在日志中写入此错误:“注意:使用未定义的常量”。
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?
这是什么意思,为什么我会看到它?
回答by Matthew Flaschen
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']);
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).
照原样,它正在寻找名为department, name, email,message等的常量。当它没有找到这样的常量时,PHP(奇怪地)将其解释为字符串(“部门”等)。显然,如果您稍后定义了这样的常量,这很容易中断(尽管使用小写常量是不好的风格)。
回答by John Carter
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.
错误消息是由于不幸的事实,即 PHP 将隐式声明未知标记为同名的常量字符串。
That is, it's trying to interpret this (note the missing quote marks):
也就是说,它试图解释这个(注意缺少的引号):
$_POST[department]
The only valid way this would be valid syntax in PHP is if there was previously a constant departmentdefined.  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:
在 PHP 中这将是有效语法的唯一有效方式是之前是否department定义了一个常量。可悲的是,此时它并没有因致命错误而死亡,而是发出此通知并表现为好像定义了一个具有相同名称和值的常量:
// Implicit declaration of constant called department with value 'department'
define('department', 'department');  
There are various ways you can get this error message, but they all have the same root cause - a token that couldbe a constant.
有多种方法可以获得此错误消息,但它们都有相同的根本原因——一个可以是常量的标记。
Strings missing quotes:  $my_array[bad_key]
缺少引号的字符串:  $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:
改变:
$department = mysql_real_escape_string($_POST[department]);
...(etc)...
To:
到:
$department = mysql_real_escape_string($_POST['department']);
...(etc)...
Variable missing dollar sign: var_without_dollar
变量缺失美元符号: 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:
您可能会看到此错误消息的另一个原因是,如果您$从变量或$this->成员中删除了 。例如,以下任一情况都会导致类似的错误消息:
my_local;   // should be $my_local
my_member;  // should be $this->my_member
Invalid character in variable name: $bad-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:
例如,这是可以的,因为变量名中允许使用下划线:
if (123 === $my_var) {
  do_something();
}
But this isn't:
但这不是:
if (123 === $my-var) {
  do_something();
}
It'll be interpreted the same as this:
它将被解释为与此相同:
if (123 === $my - var) {  // variable $my minus constant 'var'
  do_something();
}
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 global define().
为了引用类常量,您需要使用 指定类范围::,如果您错过了这一点,PHP 会认为您在谈论全局define().
Eg:
例如:
class MyClass {
  const MY_CONST = 123;
  public function my_method() {
    return self::MY_CONST;  // This is fine
  }
  public function my_method() {
    return MyClass::MY_CONST;  // This is fine
  }
  public function my_bad_method() {
    return MY_CONST;  // BUG - need to specify class scope
  }
}
Using a constant that's not defined in this version of PHP, or is defined in an extension that's not installed
使用此版本的 PHP 中未定义的常量,或未安装的扩展中定义的常量
There are some system-defined constants that only exist in newer versions of PHP, for example the mode option constants for round()such as PHP_ROUND_HALF_DOWNonly exist in PHP 5.3 or later.
有一些系统定义的常量只存在于PHP的较新版本,例如,模式选择常数round()如PHP_ROUND_HALF_DOWN只存在于PHP 5.3或更高版本。
So if you tried to use this feature in PHP 5.2, say:
因此,如果您尝试在 PHP 5.2 中使用此功能,请说:
$rounded = round($my_var, 0, PHP_ROUND_HALF_DOWN);
You'd get this error message:
您会收到此错误消息:
Use of undefined constant PHP_ROUND_HALF_DOWN - assumed 'PHP_ROUND_HALF_DOWN' Warning (2): Wrong parameter count for round()
使用未定义的常量 PHP_ROUND_HALF_DOWN - 假设为“PHP_ROUND_HALF_DOWN”警告(2):round() 的参数计数错误
回答by Giancarlo
you probably forgot to use "". 
你可能忘记使用"".
For exemple:
举个例子:
$_array[text] = $_var;
change to:
改成:
$_array["text"] = $_var;
回答by Ahmed Al-hajri
You missed putting single quotes around your array keys:
您错过了在数组键周围放置单引号:
$_POST[email]
$_POST[电子邮件]
should be:
应该:
$_POST['email']
$_POST['email']
回答by Hackbal Teamz
Insert single quotes.
插入单引号。
Example
例子
$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']); 
回答by Srihari Goud
The correct way of using post variables is
使用后变量的正确方法是
<?php
$department = $_POST['department'];
?>
Use single quotation(')
使用单引号(')
回答by santoshvijaypawar
<?php 
  ${test}="test information";
  echo $test;
?>
Notice: Use of undefined constant test - assumed 'test' in D:\xampp\htdocs\sp\test\envtheitroadnmentVariables.php on line 3 test information
注意:使用未定义的常量测试 - 在 D:\xampp\htdocs\sp\test\envtheitroadnmentVariables.php 中第 3 行测试信息假定为 'test'
回答by Jorge Vicente Mendoza
Am not sure if there is any difference am using code igniter and i use "" for the names and it works great.
我不确定使用代码点火器是否有任何区别,我使用“”作为名称,效果很好。
$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"]);
regards,
问候,
Jorge.
乔治。
回答by Venkat
Looks like the predefined fetch constants went away with the MySQL extension, so we need to add them before the first function...
看起来预定义的 fetch 常量随 MySQL 扩展一起消失了,所以我们需要在第一个函数之前添加它们......
//predifined fetch constants
//预定义的获取常量
define('MYSQL_BOTH',MYSQLI_BOTH);
define('MYSQL_NUM',MYSQLI_NUM);
define('MYSQL_ASSOC',MYSQLI_ASSOC);
I tested and succeeded.
我测试并成功了。

