PHP const and define differences and how to use them

Source: Internet
Author: User
Tags scalar

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. Use

Get_defined_constants () can get a list of all the constants that have been defined.

Constants and variables have the following differences:
• No dollar sign ($) in front of the constants;
• Constants can only be defined with the Define () function, not through assignment statements;
• Constants can be 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 Defining constants
<? Php
Define ("CONSTANT", "Hello World");
Echo CONSTANT; Outputs "Hello world."
Echo Constant; Output "Constant" and send out a hint of information
?>

Example #2 use keyword const to define constants
<? Php
The following code will work correctly after PHP 5.3.0
Const CONSTANT = ' Hello world ';
Echo CONSTANT;
?>
Example #3 Legal and illegal constant names
<? Php
A valid constant name
Define ("FOO", "something");
Define ("FOO2", "something Else");
Define ("Foo_bar", "something More");
Illegal constant name
Define ("2FOO", "something");

The following definitions are legal, but should be avoided: (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" When defining constants in PHP, what is the difference between const and define?

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 '); The effective 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); The effective valid
(6). const-defined constants are case-sensitive, and define can specify case sensitivity by using the third argument (true for case insensitivity).
For example:
Define (' FOO ', ' BAR ', true);
Echo FOO; BAR
echo foo; BAR


Related functions:

define-defines a constant

Description
BOOL Define (String $name, mixed $value [, bool $case _insensitive = false]

Parameters:
Name: constant name.
Value: constant, only 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 this is not recommended and may result in an

The state of being aware of the occurrence.
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: Case insensitive constants in lowercase

stored in the same way. )

Return value: Returns TRUE on success, or FALSE on failure.


constant-returns the value of a constant
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. The function also applies

Class constants.
Parameters:
Name: constant name.

return value:
Returns the value of a constant. Returns NULL if the constant is undefined.


defined-checks if a constant exists for a name

Description
bool Defined (string $name)

Checks if the constant for the 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 ().

Parameters:
Name: The names of the constants.

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 Constantsand their values

Returns the value of a constant name and constant in an associative array. This includes those constants created by the extension and by the Define () function.

PHP const and define differences and how to use them

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.