frrlod
frrlod

Reputation: 6685

Getting an error using constants

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 defined like this, for example:

const $humanHEALTH = 1.1;

Everything works properly running it in my localhost using WAMP, but when I upload it to an online host I get this error:

Parse error: syntax error, unexpected T_CONST

I used phpinfo() on one of the pages and the PHP version is 5.2.17.

Any help would be greatly appreciated.

Upvotes: 2

Views: 2812

Answers (4)

Leopoldo Sanczyk
Leopoldo Sanczyk

Reputation: 1609

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.

Upvotes: 0

ConcurrentHashMap
ConcurrentHashMap

Reputation: 5084

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.

Upvotes: 1

Farkie
Farkie

Reputation: 3337

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

Upvotes: 3

Marc B
Marc B

Reputation: 360572

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

Upvotes: 1

Related Questions