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:

Php Code
<?php
define("GREETING","Hi ! How are you ?",TRUE);
echo constant("GREETING");
?>
[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:

Php Code
<?php
// case-sensitive constant name
define("GREETING", "Welcome to wikitechy.com!");
echo GREETING;
?>

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

Php Code
<?php
// case-insensitive constant name
define("GREETING", "Welcome to wikitechy.com!", true);
echo greeting;
?>

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:

Php Code

<?php
define("GREETING", "Welcome to wikitechy.com!");

function myTest() {
echo GREETING;
}

myTest();
?>

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:

Php Code
const CONST_VAL = 'CONST';
define('CONST_VAL', 'CONST');
[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.

Php Code
if (...) {
const CONST_VAL = 'CONST'; // invalid
}
// but
if (...) {
define('CONST_VAL', 'CONST'); // valid
}

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

Php Code
if (!defined('CONST_VAL')) {
define('CONST_VAL', 'CONST');
}

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

Php Code
const BIT_5 = 1 << 5; // invalid
define(‘BIT_5′, 1 << 5); // valid

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

This allows to do things like this:

Php Code
for ($i = 0; $i < 32; ++$i) {
define(‘BIT_’ . $i, 1 << $i);
}

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

Php Code
define(‘FOO’, ‘BAR’, true);
echo FOO; // BAR
echo foo; // BAR
[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: