PHP constants: Difference between define and const

Source: Internet
Author: User
PHP constants: Difference between define and const
A constant is the identifier (name) of a simple value ). As its name implies, this value cannot be changed during script execution (except for so-called magic constants, they are actually not constants ). Constants are case sensitive by default. Constant identifiers are always capitalized. You can use the define () function to define constants. In PHP 5.3.0 and later versions, you can use the const keyword to define constants outside the class. in earlier versions, the const keyword can only be used in the class. Once a constant is defined, it cannot be changed or undefined. Constants can only contain scalar data (boolean, integer, float, and string ). Resource constants can be defined, but should be avoided as far as possible, because unexpected results may occur. You can simply specify its name to obtain the constant value. unlike variables, you should not add the $ symbol before the constant. If the constant name is dynamic, you can use the constant () function to obtain the constant value. Use get_defined_constants () to obtain a list of all defined constants. Constants and variables are different as follows: · constants do not have the dollar sign ($) before them. · constants can only be defined using the define () function, rather than using the value assignment statement; · constants can be defined and accessed anywhere regardless of the scope of variables. · once defined, constants cannot be redefined or undefined. · The constant value can only be a scalar. Example #1 define a constant "<? Phpdefine ("CONSTANT", "Hello world."); echo CONSTANT; // outputs "Hello world." echo Constant; // output "Constant" and send a prompt message?> "Example #2 use the keyword const to define a constant" <? Php // the following code works properly after PHP 5.3.0 const CONSTANT = 'Hello World'; echo CONSTANT ;?> "Example #3 legal and illegal constant names" <? Php // valid constant name define ("FOO", "something"); define ("FOO2", "something else"); define ("FOO_BAR ", "something more"); // The invalid constant name define ("2FOO", "something"); // The following definition is valid, but do not do this: (do not start with _ as a custom constant) // maybe one day, PHP will define a magic constant of _ FOO _. // This will conflict with your code: define ("_ FOO __", "something");?> "[Question] What is the difference between const and define when defining constants in php? [Answer] Using const makes the code simple and easy to read. const itself is a language structure, and define is a function. In addition, const is much faster than define during compilation. (1). const is used for defining class member variables. once defined, it cannot be modified. Define cannot be used to define class member variables and can be used as global constants. (2). const can be used in the class, and define cannot. (3). const cannot define constants in condition statements. Example: if (...) {const FOO = 'bar'; // invalid} if (...) {define ('foo', 'bar'); // valid} (4 ). const uses a normal constant name, and define can use an expression as the name. Const FOO = 'bar'; for ($ I = 0; $ I <32; ++ $ I) {define ('bit _'. $ I, 1 <$ I) ;}( 5 ). const can only accept static scalar, while define can use any expression. For example, const BIT_5 = 1 <5; // invalid define ('bit _ 5', 1 <5); // valid (6 ). the constants defined by const are case sensitive, while define specifies whether the values are case sensitive by using the third parameter (true indicates case insensitive. For example: define ('foo', 'bar', true); echo FOO; // BAR related function: define-defines a constant description: bool define (string $ name, mixed $ value [, bool $ case_insensitive = false] parameter: name: constant name. Value: The value of a constant. only scalar and null are allowed. The scalar type is integer, float, string, or boolean. It can also define the type of constant value as resource, but it is not recommended to do so, it may cause unknown conditions. Case_insensitive: If set to TRUE, this constant is case insensitive. The default value is case sensitive. For example, CONSTANT and Constant represent different values. (Note: constants that are case insensitive are stored in lowercase .) Return value: return TRUE if the call succeeds, or return FALSE if the call fails. constant-return the value of a constant. description: mixed constant (string $ name) returns the value of a constant through name. Constant () is useful when you do not know the constant name but need to obtain the constant value. That is, the constant name is stored in a variable, or the constant name is returned by the function. This function also applies to class constants. Parameter: name: constant name. Return value: return the constant value. If the constant is not defined, NULL is returned. Defined-check whether a constant of a name exists. description: bool defined (string $ name) checks whether the constant of the name has been defined. Note: If you want to check whether a variable exists, use isset (). The defined () function is only valid for constants. If you want to check whether a function exists, use function_exists (). Parameter: name: constant name. Return value: if the constant of the name has been defined, TRUE is returned; if not defined, FALSE is returned. Get_defined_constants: Returns an associative array with the names of all the constants and their values Returns the constant name and constant value in the correlated array. This includes constants created by extensions and the define () function. Reference link: Difference between defining constants define and const 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.