Compared to a variable, a constant’s value can not be changed during the execution of the script. In PHP, you can define a constant with the define() function. Generally, a constant’s name use uppercase representation. The content is mainly organized from PHP Manual.
There are somethings to be aware of:
- A constant’s scope is global even it is defined inside a function.
> About class constants
>
> As of PHP 7.1.0 class constants may declare a visibility of protected or private to limit their availability likeprivate const COLOR = 'red'
.
>
> See more at PHP: Class Constants. -
Reserved constant start with
__
. It’s better not to define constants with__
started.
Constant
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.
echo VERSION; // 1.0
echo MAX_VALUE; // 100
echo PATH; // D:programsphpworkspacelanguage
// Works as of PHP 7
// Define an array constant
define('COLORS', array(
'red',
'green',
'blue'
));
echo COLORS[1] . EOL; // outputs "green"
define
vsconst
in a classclass MyClass { const TUESDAY = 'Tuesday'; public static function foo() { define('MONDAY', 'Monday'); } } // echo MONDAY . "n"; // Warning: Use of undefined constant MONDAY echo MyClass::TUESDAY . "n"; $obj = new MyClass(); $obj->foo(); echo MONDAY . "n"; // The scope of MONDAY is global.
Check whether a constant is defined
Use defined()
)to check Checks whether a given named constant exists.
// Check whether a named constant exists.
// Remember to use the quotes.
if (defined('MONDAY')) {
echo MONDAY;
}
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. Usuallyint
(2147483647) in 32 bit systems andint
(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 magic constants which will 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. |
Example
define( 'PATH', __DIR__ );
require_once( 'PATH' . '/inc.php' );