Home > Backend Development > PHP Tutorial > The difference between const and define in php (supplement)

The difference between const and define in php (supplement)

WBOY
Release: 2016-07-30 13:30:39
Original
869 people have browsed it

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

1 <?php

2define("CONSTANT" , "Hello world.");

3echoCONSTANT; // outputs "Hello world."

4echoConstant; // Output "Constant" and send a prompt message

5?>

Example #2 Using the keyword const Define constants

1 <?php

2 //The following code can work normally after PHP 5.3.0

3constCONSTANT = 'Hello World';

4echoCONSTANT;

5
?&gt ;
Example #3 Legal and illegal constant names

01
<?php

02
// Legal constant name

03"FOO"
define(, ) "something");

04"FOO2"
define(, "something else");

05"FOO_BAR"
define(, "something more");

06
//Illegal constant name

07"2FOO"
define("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

11"__FOO__"
define(, "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:

1 if(...){

2 constFOO = 'BAR'; // invalid invalid

3 }

4 if(...) {

5 ) define('FOO', 'BAR'); // valid valid

6 }

(4).const takes a Ordinary constant names, define can use expressions as names.

1 const FOO = 'BAR';

2 for($i= 0; $i< 32; ++ $i) {

4
$i, 1 << $i);

}

For example:
(5).const can only accept static scalars, but define can Take any expression.

1

const

BIT_5 = 1 << 5; 2
// Invalid invalid

define(

'BIT_5'For example:
, 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.

1

define(

'FOO'2
, 'BAR' , true);

echo

FOO; 3
// BAR

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>
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template