Difference between const and define in php (supplement)

Source: Internet
Author: User
: This article mainly introduces the difference between const and define in php (supplement). If you are interested in PHP tutorials, refer to it. 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:

  • There is no dollar sign before the constant ($ );
  • Constants can only be defined using the define () function, but cannot be defined using the value assignment statement;
  • Constants can be defined and accessed anywhere regardless of the scope of variables;
  • Once defined, a constant cannot be redefined or canceled;
  • The constant value can only be a scalar.

Example #1 Define constants

1

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

3 echoCONSTANT;// outputs "Hello world."

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

5 ?>

Example #2 use the keyword const to define constants

1

2 // The following code works properly after PHP 5.3.0

3 constCONSTANT ='Hello World';

4 echoCONSTANT;

5 ?>

Example #3 valid and invalid constant names

01

02 // Valid constant name

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

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

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

06 // The constant name is invalid.

07 define("2FOO", "something");

08 // The following definition is valid, but it should be avoided: (do not start with _ as a custom constant)

09 // Maybe one day, PHP will define a magic constant of _ FOO _.

10 // This will conflict with your code

11 define("__FOO__","something");

12 ?>

The difference between const and define when defining constants in PHP:

Using const makes the code 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.

For example:

1 if(...){

2 constFOO = 'BAR'; // Invalid

3 }

4 if(...) {

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

6 }

(4). const uses a normal constant name, and define can use an expression as the name.

1 const FOO ='BAR';

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

3 define('BIT_'. $i, 1 <<$i);

4 }

(5). const can only accept static scalar, while define can use any expression.

For example:

1 constBIT_5 = 1 << 5; // Invalid

2 define('BIT_5', 1 << 5);// Valid

(6). const defines constants in upper case and lower case sensitivity, while define specifies whether the values are case sensitive by using the third parameter (true indicates case insensitive.

For example:

1 define('FOO','BAR', true);

2 echoFOO; // BAR

3 echofoo; // BAR

Related functions:

Define-define a constant

Note:

Bool define (string $ name, mixed $ value [, bool $ case_insensitive = false]

Parameters:

Name: constant name.

Value: The value of a constant. only scalar and null are allowed. The scalar type is integer, float, string, or boolean. You can also define the constant value type as resource, but it is not recommended to do so.
The occurrence of known 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 in lowercase.
.)

Return value: TRUE is returned when the request is successful, or FALSE is returned when the request fails.

Constant-returns the value of a constant.

Note:

Mixed constant (string $ name)

Returns the constant value 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 is also applicable
Class constants.

Parameters:

Name: constant name.

Return value:

Returns the value of a constant. If the constant is not defined, NULL is returned.

Defined-check whether a constant of a name exists

Note:

Bool defined (string $ name)

Check 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 ().

Parameters:

Name: constant name.

Return value:

If the constant of the name has been defined, TRUE is returned; otherwise, 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 an associated array. This includes constants created by extensions and the define () function.

The above introduces the difference between const and define in php (supplement), including some content, and hopes to help friends who are interested in PHP tutorials.

Related Article

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.