A constant is an identifier (name) of a simple value. As the name implies, the value cannot change during script execution (except for so-called magic constants, which are not constants). Constants are case-sensitive by default. Normally 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. In previous versions, the const keyword could only be used within a class. Once a constant is defined, it cannot be changed or undefined.
Constants can only contain scalar data (boolean, integer, float and string). It is possible to define resource constants, but this should be avoided as it can cause unpredictable results.
You can get the value of a constant simply by specifying its name. Unlike variables, you should not add the $ sign in front of a constant. If the constant name is dynamic, you can also use the function constant() to get the value of the constant. Use get_defined_constants() to get a list of all defined constants.
The differences between constants and variables are as follows:
- There is no dollar sign ($) in front of constants;
- Constants can only be defined with the define() function, not through assignment statements;
- Constants can be used regardless of the scope of the variable. Define and access anywhere;
- Once defined, a constant cannot be redefined or undefined;
- The value of a constant can only be a scalar.
Example #1 Define constants
2 | define( "CONSTANT" ,
"Hello world." ); |
3 | echo CONSTANT;
// outputs "Hello world." |
4 | echo Constant;
// Output "Constant" and send a prompt message |
Example #2 Using the keyword const Define constants
2 | //The following code can work normally after PHP 5.3.0 |
3 | const CONSTANT =
'Hello World' ; |
Example #3 Legal and illegal constant names
02 // Legal constant name |
|
03define( | "FOO", )
"something" );
|
04define( | "FOO2",
"something else" );
|
05define( | "FOO_BAR",
"something more" );
|
06//Illegal constant name |
|
07 define( | "2FOO",
"something" );
|
08// The following definition is legal, but should be avoided: (Custom constants do not start with __ ) |
|
09// Maybe one day in the future PHP will define a __FOO__ magic constant |
|
10 // This will conflict with your code |
|
11define( | "__FOO__",
"something" );
|
12?> |
When defining constants in PHP, the difference between const and define:
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 the definition of class member variables. Once defined, it cannot be modified. define cannot be used for the definition of class member variables, but can be used for global constants.
(2).const can be used in classes, define cannot.
(3).const cannot define constants in conditional statements.
For example:
2
|
const FOO = 'BAR' ;
// invalid invalid
|
5 |
) define( 'FOO' ,
'BAR' ); // valid valid
|
(4).const takes a Ordinary constant names, define can use expressions as names.
2 |
for ( $i = 0; $i < 32; ++ $i ) {
|
}
|
(5).const can only accept static scalars, but define can Take any expression.
| For example:
1
const
BIT_5 = 1 << 5;
// Invalid invalid
|
|
2
define(
'BIT_5'
, 1 << 5);
//Effective valid |
(6).const defined constants are case-sensitive, and define can specify the size through the third parameter (true means case-insensitive) Write whether it is sensitive.
| For example:
1
define(
echo
echo
foo;
// BAR
|
<p>Related functions: </p>
<p><span><span>define — Define a constant </span></span></p>
<p> Description: </p>
<p> bool define ( string $name , mixed $value [, bool $case_insensitive = false ] </p>
<p> Parameters: </p>
<p> name : Constant name. </p>
<p> value: only scalar and null are allowed. The type of scalar is integer, float, string or boolean. It is also possible to define the type of constant value as resource, but this is not recommended and may cause unexpected problems. <br></p> case_insensitive: If set to TRUE, the constant is case-insensitive. For example, CONSTANT and Constant represent different values. (Note: Case-insensitive constants. Stored in lowercase <p>)<br></p>Return value: TRUE on success, or FALSE on failure.<p></p>
<p><span>constant — Returns a constant value<span></span></span></p>Description: <p></p> mixed constant ( string $name )<p></p> Returns the value of a constant by name. Constant() is useful when you don’t know the name of the constant but need to get the value of the constant. That is, the constant name is stored in a variable, or the constant name is returned by a function. This function also works with <p>class constants. <br></p> name: constant name. <p></p> Returns NULL if the constant is not defined. <p></p>
<p></p>defined — Check the value of the constant. Whether the constant exists <p></p>
<p><span> Description: <span></span> bool defined ( string $name ) </span></p> Check whether the constant of this name is defined. <p></p> Note: If you want to check whether a variable exists, use definedisset(). ) function is only valid for constants. If you want to check whether a function exists, use function_exists(). <p></p> Parameters: <p></p> Name: The name of the constant. Return value: <p></p> Returns TRUE if the constant with this name is defined. ; Returns FALSE if not defined. <p></p>
<p></p>get_defined_constants:<p></p>
<p></p>Returns an associative array with the names of all the constants and their values<p>Returns the constant name and the value of the constant in an associative array. This includes those constants created by extensions and by the define() function
<span>
The above has introduced the difference between const and define in PHP (supplementary), including relevant content. I hope it will be helpful to friends who are interested in PHP tutorials. <span>
</span>
</span></p> |
|