Getting an error using constants

后端 未结 4 1523
南旧
南旧 2020-12-12 01:26

I have a lot of pages, all of which require the file characters.php. This file contains constants which define many things in my website. They are

相关标签:
4条回答
  • 2020-12-12 02:03

    Support for const outside of class definitions was not added until PHP 5.3, so your 5.2.x is too old to use this. See http://php.net/const

    0 讨论(0)
  • 2020-12-12 02:15

    In PHP 5.2 it's define('constant_name', 'value');

    0 讨论(0)
  • 2020-12-12 02:21

    A constant must not have any $ sign at the beginning. Try const HUMAN_HEALTH = 1.1 instead.

    As Marc B mentioned, const outside classes is only available up from PHP 5.3.

    0 讨论(0)
  • 2020-12-12 02:24

    To add possible causes to those already mentioned: use of the $ character, and old PHP version; I leave this clarification from the manual that was useful to detect the problem in my case:

    As opposed to defining constants using define(), constants defined using the const keyword must be declared at the top-level scope because they are defined at compile-time. This means that they cannot be declared inside functions, loops, if statements or try/ catch blocks.

    0 讨论(0)
提交回复
热议问题