PHP 错误消息“使用未定义常量”是什么意思?意思是?

发布于 2024-09-04 06:30:26 字数 692 浏览 9 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神爱温柔 2024-09-11 06:30:26

您的数组键是字符串。 PHP 中的字符串文字必须加引号:

$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 的常量。 code> 等。当它找不到这样的常量时,PHP(奇怪地)将其解释为字符串(“部门”等),但会对此发出警告。显然,如果您稍后定义这样的常量,这很容易被破坏(尽管使用小写常量是不好的风格)。

更新:此行为后来在 PHP 8 中得到修复:“如果使用未定义的常量,则会引发错误。” (PHP:语法 - 手册

Your array keys are strings. String literals must be quoted in PHP:

$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), 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)

挽梦忆笙歌 2024-09-11 06:30:26

该错误消息是由于 PHP 会隐式将未知标记声明为同名常量字符串而导致的。

也就是说,它试图解释这一点(注意缺少的引号):

$_POST[department]

在 PHP 中,唯一有效的语法是如果之前定义了一个常量 department 。遗憾的是,它并没有在此时因致命错误而死亡,而是发出此通知,并表现得好像已使用相同的名称和值定义了一个常量:

// Implicit declaration of constant called department with value 'department'
define('department', 'department');  

您可以通过多种方式获取此错误消息,但它们都有相同的根本原因 - 可以是常量的标记。

字符串缺少引号: $my_array[bad_key]

这就是您的情况的问题所在,这是因为您的字符串数组键尚未被引用。修复字符串键将修复该错误:

更改:更改

$department = mysql_real_escape_string($_POST[department]);
...(etc)...

为:

$department = mysql_real_escape_string($_POST['department']);
...(etc)...

变量缺少美元符号:var_without_dollar

您可能会看到此错误消息的另一个原因是,如果您在变量,或来自成员的 $this->。例如,以下任一情况都会导致类似的错误消息:

my_local;   // should be $my_local
my_member;  // should be $this->my_member

变量名称中的无效字符:$bad-variable-name

如果您尝试在变量中使用不允许的字符,则可能会导致类似但更微妙的问题。变量名 - 连字符 (-) 而不是下划线 _ 是常见情况。

例如,这是可以的,因为 变量名称中允许使用下划线

if (123 === $my_var) {
  do_something();
}

但这不是:

if (123 === $my-var) {
  do_something();
}

它将被解释为与此相同:

if (123 === $my - var) {  // variable $my minus constant 'var'
  do_something();
}

引用类常量而不指定类范围

为了引用类常量,您需要使用 ::< 指定类范围/code>,如果你错过了这一点,PHP 会认为你正在谈论一个全局 define()

例如:

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
  }
}

使用未在此版本的 PHP 中定义的常量,或者在未安装的扩展中定义的常量

有一些系统定义的常量仅存在于较新版本的 PHP 中,例如 round() 例如 PHP_ROUND_HALF_DOWN存在于 PHP 5.3 或更高版本中。

因此,如果您尝试在 PHP 5.2 中使用此功能,请说:

$rounded = round($my_var, 0, PHP_ROUND_HALF_DOWN);

您会收到以下错误消息:

使用未定义常量 PHP_ROUND_HALF_DOWN - 假设
'PHP_ROUND_HALF_DOWN' 警告(2):round() 参数计数错误

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):

$_POST[department]

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:

// 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 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:

$department = mysql_real_escape_string($_POST[department]);
...(etc)...

To:

$department = mysql_real_escape_string($_POST['department']);
...(etc)...

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:

my_local;   // should be $my_local
my_member;  // should be $this->my_member

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:

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().

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

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_DOWN only exist in PHP 5.3 or later.

So if you tried to use this feature in PHP 5.2, say:

$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()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文