?> |
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> |
|