PHP: Constants

You can create a Constant variable within a Class:

class Automobile {
    const AVG_COST = 5000;
}

AVG_COST will be static (you don’t need to instantiate an Automobile object to access it):

if (cost < Automobile::AVG_COST) {
    ...
}

You can also create a Constant variable with GLOBAL scope using the define method:

<?php

define('HELLO_MSG', "Hello!");

Warning!!! Constants declared using define have a GLOBAL scope! You should probably avoid for all but a few special cases.

The defined function returns 1 if a constant name has been defined, and nothing if not:

defined('HELLO_MSG');