What Does the PHP Error Message "Notice: Use of Undefined Constant" Mean

What does the PHP error message Notice: Use of undefined constant mean?

department is meant to be a string (to be used here as array key). Strings in PHP must be enclosed in quotes. In the days of the long past PHP was lax about using quotes for single-word strings but these days are long gone.

Therefore, it must be 'department' or "department".

The same goes for the other errors as well.

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 defined such a constant later (though it's bad style to have lower-case constants).

Undefined constant error in php 7.2

This is a common warning that occurs whenever PHP has detected the usage of an undefined constant.

Here is an example of constant being defined in PHP:

define('PI', 3.14);

Below is a list of some cases that might cause the issue:

  • Forgetting to use a $ symbol at the start of a variable name.

    $name = "Aniket";
    echo name; // forgot to add $ before name

    The above code will throw: Notice: Use of undefined constant name – assumed ‘name’. Because there is no dollar sign in front of the variable “name”, PHP assumes that I was trying to reference a constant variable called “name”.

  • Forgetting to place quotes around strings.

    echo $_POST[email];

    In the example above, I failed to place quotes around the $_POST variable “email”. This code will throw: Notice: Use of undefined constant name – assumed ’email’.

    To fix this, I’d obviously have to do the following:

    echo $_POST["email"];

According to Deprecated features in PHP 7.2.x you should not use undefined constants because:

Unquoted strings that are non-existent global constants are taken to be strings of themselves.

This behaviour used to emit an E_NOTICE, but will now emit an E_WARNING. In the next major version of PHP, an Error exception will be thrown instead.

You can prevent this E_WARNING only if you declare the constant value before using it.

In the above question, MODULE_HEADER_SELECT_TEMPLATE_STATUS is not defined.



Related Topics



Leave a reply



Submit