PHP Error: Use of Undefined Constant
When PHP encounters the error message "Notice: Use of undefined constant," it indicates that a value used in the code is not defined as a constant. This can occur when a variable name is misspelled, or when the constant has not been declared.
In your specific example, the error occurs because the array keys in the following code are written as strings without quotes:
$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]);
To resolve this issue, enclose the array keys in quotes to mark them as strings. This informs PHP that these are variable names, not constants:
$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']);
Previously, PHP interpreted the unquoted keys as constants, which resulted in the warning messages. However, this behavior has been corrected in PHP 8, where using an undefined constant will now throw an error.
The above is the detailed content of Why Am I Getting a PHP 'Notice: Use of undefined constant' Error?. For more information, please follow other related articles on the PHP Chinese website!