Undefined constant error in php 7.2

后端 未结 3 1072
無奈伤痛
無奈伤痛 2020-12-14 03:07

I have theses errors in php v7.2 but don\'t see any E_WARNING when using php v7.1.

How can I resolve following errors?

3条回答
  •  臣服心动
    2020-12-14 03:34

    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.

提交回复
热议问题