PHP Constant

Compared to a variable, a constant’s value can not be changed during the execution of the script. Use define to define a constant. Generally, a constant’s name use uppercase representation.

  • A constant’s scope is global.
  • Reserved constant start with __. It’s better not to define constants with __ started.

Define constant

define('VERSION', '1.0');
define('MAX_VALUE', 100 );
define('PATH', __DIR__ ); // __FILE__ is a magic constant,it is the current file's directory.

Predefined constants

PHP provides a large number of predefined constants to any script which it runs.

  • TRUE (boolean)
  • FALSE (boolean)
  • NULL (null)
  • PHP_INT_SIZE (integer). The size of an integer in bytes in this build of PHP. Available since PHP 5.0.5
  • PHP_INT_MAX (integer). The largest integer supported in this build of PHP. Usually int(2147483647) in 32 bit systems and int(9223372036854775807) in 64 bit systems. Available since PHP 5.0.5
  • PHP_FLOAT_MAN (float)
  • PHP_FLOAT_MIN (float)
  • PHP_EOL (string). The correct ‘End Of Line’ symbol for this platform.
    Available since PHP 5.0.2
  • PHP_VERSION (string), the current PHP version
  • PHP_VERSION_ID (integer). The current PHP version as an integer, useful for version comparisons (e.g., int(50207) from version “5.2.7-extra”).
    Available since PHP 5.2.7.
  • PHP_OS (string). The operation system family PHP was built for.One of
    ‘Windows’, ‘BSD’,
    ‘Darwin’, ‘Solaris’,
    ‘Linux’ or ‘Unknown’.
    Available as of PHP 7.2.0.
  • E_ERROR (integer). Error reporting constant。
  • E_WARNING (integer)
  • E_NOTICE (integer)
  • E_ALL (integer)

See reserved constants for a full list of predefined constants.

Examples

// Check OS
if ( PHP_OS == 'Windows' ) {
    echo 'this is a windows system';
}

// Write 2 lines of content to a file
$content = 'The first line.' . PHP_EOL;
$content .= 'The second line.' . PHP_EOL . PHP_EOL;
file_put_contennts ( 'test.txt', $content );

Magic constants

There are nine magical constants that change depending on where they are used. Therefore they aren’t actually constants.

Unlike regular constants which are resolved at runtime, magic constants are resolved at compile time.

Note: Magic constants are case-insensitive.

Name Description
__LINE__ The current line number of the file.
__FILE__ The full path and filename of the file with symlinks resolved. If used inside an include, the name of the included file is returned.
__DIR__ The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(__FILE__). This directory name does not have a trailing slash unless it is the root directory.
__FUNCTION__ The function name, or {closure} for anonymous functions.
__CLASS__ The class name. The class name includes the namespace it was declared in (e.g. FooBar). Note that as of PHP 5.4 __CLASS__ works also in traits. When used in a trait method, __CLASS__ is the name of the class the trait is used in.
__TRAIT__ The trait name. The trait name includes the namespace it was declared in (e.g. FooBar).
__METHOD__ The class method name.
__NAMESPACE__ The name of the current namespace.
ClassName::class The fully qualified class name. See also ::class.

Examples

define( 'PATH', __DIR__ );

require_once( 'PATH' . '/inc.php' );