PHP Constants in detail: the difference between define and const

Source: Internet
Author: User
    A constant is an identifier (first name) of a simple value. As the name implies, the value cannot be changed during script execution (except for the so-called magic constants, they are not constants). Changshime is considered to be case sensitive.    Usually, constant identifiers are always uppercase. You can use the Define () function to define constants. After PHP 5.3.0, you can use the Const keyword to define constants outside the class definition, and the previous version of the Const keyword can only be used in class.    Once a constant is defined, it can no longer be changed or undefined. Constants can only contain scalar data (boolean,integer,float and string).    You can define resource constants, but avoid them as much as possible, because they cause unpredictable results. You can simply get the value of a constant by specifying its name, and unlike a variable, you should not precede the constant with the $ symbol. If the constant name is dynamic, you can also use the function constant () to get the value of the constant. A list of all defined constants can be obtained with get_defined_constants (). Constants and variables are different: • There is no dollar sign ($) in front of constants; • Constants can only be defined with the Define () function, not by an assignment statement; • Constants are defined and accessed anywhere, regardless of the scope of the variable; • Constants cannot be redefined or undefined once defined; The value of a constant can only be scalar. Example #1 define constant ' ' <? Phpdefine ("CONSTANT", "Hello World"); Echo CONSTANT; Outputs "Hello world." Echo Constant; Output "Constant" and issue an informational message?> ' Example #2 use the keyword const to define the constant ' ' <? php//The following code will work correctly after PHP 5.3.0 const CONSTANT = ' Hello world '; Echo CONSTANT; > ' Example #3 Legal and illegal constant name ' ' <? php//valid constant name define ("FOO", "something");d efine ("FOO2", "Something Else");d efine ("Foo_bar", "something More");//illegal often Volume name define ("2FOO", "something");//The definition below is legal, but should beThis avoids the following: (Custom constants do not start with __)//Maybe someday php will define a __foo__ magic constant//This will conflict with your code define ("__foo__", "something"); > "Q" What is the difference between const and define when defining constants in PHP? The "answer" uses the const to make the code easy to read, and the const itself is a language structure, and define is a function. In addition, const is much faster to compile than define. (1). Const is used to define a class member variable, which is not modifiable once defined. Define is not available for definition of class member variables and can be used for global constants. (2). Const can be used in a class, define cannot. (3). Const cannot define a constant in a conditional statement. For example: if (...)    {Const FOO = ' BAR '; Invalid invalid} if (...) {define (' FOO ', ' BAR ');//Valid valid}    (4). Const takes an ordinary constant name, and define can use an expression as its name.       Const FOO = ' BAR ';      for ($i = 0; $i < + + $i) {define (' Bit_ '. $i, 1 << $i); } (5). Const can only accept static scalars, and define may take any expression.    For example: const BIT_5 = 1 << 5; Invalid invalid define (' Bit_5 ', 1 << 5); Valid valid (6). const-defined constants are case-sensitive, and define can specify case sensitivity by a third parameter, which is true for case insensitivity.       For example: Define (' FOO ', ' BAR ', true); Echo FOO; BAR echo foo; BAR correlation function: define-defines a constant description: bool Define (String $name, mixed $value [, bool $case _insensitive = false] Parameter: Name: constant  The volume name. Value: oftenOnly scalar and null are allowed. The type of scalar is integer, Float,string, or Boolean.  It is also possible to define a constant value of type resource, but it is not recommended to do so, which can lead to the occurrence of an unknown condition. Case_insensitive: If set to TRUE, the constant is case insensitive. The default is case-sensitive. For example, CONSTANT and CONSTANT represent different values. (Note: The case-insensitive constants are stored in lowercase.) Return value: Return TRUE on success, or return false.constant-on failure returns a constant value description: Mixed constant (string $name) returns the value of a constant by name. Constant () is useful when you do not know the constant name but need to get the value of the constant. That is, the constant name is stored in a variable, or the function returns a constant name. This function also applies to class constants. Parameter: Name: constant name. Return value: Returns the value of the constant. Returns NULL if the constant is undefined.  defined-checks if a constant for a name exists description: bool defined (string $name) checks to see if the constant for that name is defined. Note: If you want to check if a variable exists, use Isset (). The defined () function is only valid for constants. If you want to detect if a function exists, use function_exists (). Parameter: Name: Constant. Return value: Returns TRUE if the constant of the name is defined, or FALSE if undefined. Get_defined_constants:returns an associative array with the names of all the constants and their values returns the value of the constant name and constant in an associative array. This includes those constants created by the extension and by the Define () function. Reference Link: Defines constants define and const differences in PHP http://www.dewen.org/q/4280
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.