PHP define() Function

Definition and Usage:

The define() function defines a constant.

Constants are much like variables, except for the following differences:

  • A constant’s value cannot be changed after it is set
  • Constant names do not need a leading dollar sign ($)
  • Constants can be accessed regardless of scope
  • Constant values can only be strings and numbers

Example:

[pastacode lang=”php” manual=”%3C%3Fphp%0Adefine(%22GREETING%22%2C%22Hi%20!%20How%20are%20you%20%3F%22%2CTRUE)%3B%0Aecho%20constant(%22GREETING%22)%3B%0A%3F%3E%0A” message=”Php Code” highlight=”” provider=”manual”/] [ad type=”banner”]

Syntax:

define(name,value,case_insensitive)

Parameter Description
name Required. Specifies the name of the constant
value Required. Specifies the value of the constant
case_insensitive Optional. Specifies whether the constant name should be case-insensitive. Possible values: TRUE – Case insensitive FALSE – Default. Case-sensitive

The example below creates a constant with a case-sensitive name:

Example:

[pastacode lang=”php” manual=”%3C%3Fphp%0A%2F%2F%20case-sensitive%20constant%20name%0Adefine(%22GREETING%22%2C%20%22Welcome%20to%20wikitechy.com!%22)%3B%0Aecho%20GREETING%3B%0A%3F%3E%20%0A” message=”Php Code” highlight=”” provider=”manual”/]

The example below creates a constant with a case-insensitive name:

[pastacode lang=”php” manual=”%3C%3Fphp%0A%2F%2F%20case-insensitive%20constant%20name%0Adefine(%22GREETING%22%2C%20%22Welcome%20to%20wikitechy.com!%22%2C%20true)%3B%0Aecho%20greeting%3B%0A%3F%3E%20%0A” message=”Php Code” highlight=”” provider=”manual”/]

Constants are Global :

Constants are automatically global and can be used across the entire script.

Below example we uses a constant inside a function, even if it is defined outside the function:

[pastacode lang=”php” manual=”%0A%3C%3Fphp%0Adefine(%22GREETING%22%2C%20%22Welcome%20to%20wikitechy.com!%22)%3B%0A%0Afunction%20myTest()%20%7B%0A%20%20%20%20echo%20GREETING%3B%0A%7D%0A%20%0AmyTest()%3B%0A%3F%3E%20%0A” message=”Php Code” highlight=”” provider=”manual”/]

define() vs const

As of PHP 5.3 there are two ways to define constants:

Either using the const keyword or using the define() function:

[pastacode lang=”php” manual=”const%20CONST_VAL%20%3D%20’CONST’%3B%0Adefine(‘CONST_VAL’%2C%20’CONST’)%3B%0A” message=”Php Code” highlight=”” provider=”manual”/] [ad type=”banner”]

The fundamental difference between those two ways is that const defines constants at compile time, whereas define defines them at run time.

const cannot be used to conditionally define constants. It has to be used in the outermost scope.

[pastacode lang=”php” manual=”if%20(…)%20%7B%0Aconst%20CONST_VAL%20%3D%20’CONST’%3B%20%20%20%20%2F%2F%20invalid%0A%7D%0A%2F%2F%20but%0Aif%20(…)%20%7B%0Adefine(‘CONST_VAL’%2C%20’CONST’)%3B%20%2F%2F%20valid%0A%7D%0A” message=”Php Code” highlight=”” provider=”manual”/]

One common application is to check whether the constant is already defined:

[pastacode lang=”php” manual=”if%20(!defined(‘CONST_VAL’))%20%7B%0Adefine(‘CONST_VAL’%2C%20’CONST’)%3B%0A%7D%0A” message=”Php Code” highlight=”” provider=”manual”/]

const accepts a static scalar (number, string or other constant like true, false, null, __FILE__), whereas define() takes any expression:

[pastacode lang=”php” manual=”const%20BIT_5%20%3D%201%20%3C%3C%205%3B%20%2F%2F%20invalid%0Adefine(%E2%80%98BIT_5%E2%80%B2%2C%201%20%3C%3C%205)%3B%20%2F%2F%20valid%20%0A” message=”Php Code” highlight=”” provider=”manual”/]

const takes a plain constant name, whereas define() accepts any expression as name. 

This allows to do things like this:

[pastacode lang=”php” manual=”for%20(%24i%20%3D%200%3B%20%24i%20%3C%2032%3B%20%2B%2B%24i)%20%7B%0Adefine(%E2%80%98BIT_%E2%80%99%20.%20%24i%2C%201%20%3C%3C%20%24i)%3B%0A%7D%0A” message=”Php Code” highlight=”” provider=”manual”/]

consts are always case sensitive, whereas define() allows you to define case insensitive constants by passing true as the third argument:

[pastacode lang=”php” manual=”define(%E2%80%98FOO%E2%80%99%2C%20%E2%80%98BAR%E2%80%99%2C%20true)%3B%0Aecho%20FOO%3B%20%2F%2F%20BAR%0Aecho%20foo%3B%20%2F%2F%20BAR%0A” message=”Php Code” highlight=”” provider=”manual”/] [ad type=”banner”]

Now let’s look at the reason why we always use const unless one of the above situations occurs:

const simply reads nicer. It’s a language construct instead of a function and also is consistent with how you define constants in classes.
As consts are language constructs and defined at compile time they are a bit faster than define()s.

It is well known that PHP define()s are slow when using a large number of constants.

People have even invented things like apc_load_constants() and hidef to get around this.

consts make the definition of constants approximately twice as fast (on development machines with XDebug turned on even more). Lookup time on the other hand does not change (as both constant types share the same lookup table): Demo.

Categorized in: