Constants A constant is an identifier (name) for a simple value. As the name suggests, that value cannot change during the execution of the script (except for magic constants, which aren't actually constants). Constants are case-sensitive. By convention, constant identifiers are always uppercase. Prior to PHP 8.0.0, constants defined using the define function may be case-insensitive. The name of a constant follows the same rules as any label in PHP. A valid constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thusly: ^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$ It is possible to define constants with reserved or even invalid names, whose value can only be retrieved with the constant function. However, doing so is not recommended. &tip.userlandnaming; Valid and invalid constant names ]]> For our purposes here, a letter is a-z, A-Z, and the ASCII characters from 128 through 255 (0x80-0xff). Like &link.superglobals;, the scope of a constant is global. Constants can be accessed from anywhere in a script without regard to scope. For more information on scope, read the manual section on variable scope. As of PHP 7.1.0, class constant may declare a visibility of protected or private, making them only available in the hierarchical scope of the class in which it is defined. Syntax Constants can be defined using the const keyword, or by using the define-function. While define allows a constant to be defined to an arbitrary expression, the const keyword has restrictions as outlined in the next paragraph. Once a constant is defined, it can never be changed or undefined. When using the const keyword, only scalar (bool, int, float and string) expressions and constant arrays containing only scalar expressions are accepted. It is possible to define constants as a resource, but it should be avoided, as it can cause unexpected results. The value of a constant is accessed simply by specifying its name. Unlike variables, a constant is not prepended with a $. It is also possible to use the constant function to read a constant's value if the constant's name is obtained dynamically. Use get_defined_constants to get a list of all defined constants. Constants and (global) variables are in a different namespace. This implies that for example &true; and $TRUE are generally different. If an undefined constant is used an Error is thrown. Prior to PHP 8.0.0, undefined constants would be interpreted as a bare word string, i.e. (CONSTANT vs "CONSTANT"). This fallback is deprecated as of PHP 7.2.0, and an error of level E_WARNING is issued when it happens. Prior to PHP 7.2.0, an error of level E_NOTICE has been issued instead. See also the manual entry on why $foo[bar] is wrong (unless bar is a constant). This does not apply to (fully) qualified constants, which will always raise a Error if undefined. To check if a constant is set, use the defined function. These are the differences between constants and variables: Constants do not have a dollar sign ($) before them; Constants may be defined and accessed anywhere without regard to variable scoping rules; Constants may not be redefined or undefined once they have been set; and Constants may only evaluate to scalar values or arrays. Defining Constants ]]> Defining Constants using the <literal>const</literal> keyword ]]> As opposed to defining constants using define, constants defined using the const keyword must be declared at the top-level scope because they are defined at compile-time. This means that they cannot be declared inside functions, loops, if statements or try/ catch blocks. &reftitle.seealso; Class Constants Predefined constants PHP provides a large number of predefined constants to any script which it runs. Many of these constants, however, are created by various extensions, and will only be present when those extensions are available, either via dynamic loading or because they have been compiled in. Magic constants There are nine magical constants that change depending on where they are used. For example, the value of __LINE__ depends on the line that it's used on in your script. All these "magical" constants are resolved at compile time, unlike regular constants, which are resolved at runtime. These special constants are case-insensitive and are as follows: PHP's magic constants &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. Foo\Bar). 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. Foo\Bar). __METHOD__ The class method name. __NAMESPACE__ The name of the current namespace. ClassName::class The fully qualified class name.
&reftitle.seealso; ::class get_class get_object_vars file_exists function_exists