Some differences between using define () and const to define constants in PHP

Source: Internet
Author: User
Tags arithmetic operators bitwise operators
As we all know, in PHP (PHP 4 and later), we can use the function define () to define constants, for example:

<?phpdefine (' PI ', 3.14159);  Defines a constant echo pi called pi;    Output:3.14159?>

However, after PHP 5.3.0, in addition to using function define (), we can also use the PHP keyword const to define constants.

For example:

<?php//The following code is required to run the const PI = 3.14159 in PHP 5.3.0 and later versions; Use the Const keyword to define a constant echo pi called pi;    Output:3.14159?>

Although constants can be defined in both of these ways, what is the difference between them? Let's take one by one to explain the difference between the Define () function in PHP and the const keyword definition constants:

1. Version differences
First, there is no doubt that there is a version difference between the two ways of defining constants, function define () can be used in both PHP4 and PHP5, and the keyword const can only be used in PHP 5.3.0 and later versions.

2. Defining the difference between locations
Because the constants defined by function define () are defined when the Define () function is executed, you can use the Define () function to define constants anywhere within a function, within a loop, within an if statement, and so on, where functions can be called. Unlike define (), because the Const keyword defines constants that are defined at compile time, the Const keyword definition constants must be in the topmost area of action. This also means that constants cannot be defined within a function, within a loop, or within an if statement with a const.

<?php//using the Const keyword to define constants must be at the top of the range//That is, you can directly parse the definition at compile time. Const DEMO = ' demo '; class person{    const man = ' male ';    Const WOMAN = ' woman ';} Interface usb{    Const VERSION_2 = ' 2.0 ';    Const VERSION_3 = ' 3.0 ';}? >

3. Differences in support for value expressions
Although constant values defined by the keyword const and define () can only be null or scalar data (boolean,integer,float and string types) and resource types (constants that define the resource type are not recommended, Otherwise, unpredictable results may occur). However, because the keyword Const definition constants are defined at compile time, many operators, such as arithmetic operators, bitwise operators, comparison operators, and so on, are not supported in expressions for constant values defined by the Const keyword, and these operators can be used directly when defining constants in the Define () function.

<?phpdefine (' define_var1 ', 1 << 1);//const const_var1 = (1 << 1); Const does not support bitwise operators, PHP will report syntax errors DEFINE (' define_var2 ', 1 + 1);//const const_var2 = 1 + 1; Const does not support arithmetic operators, PHP will report syntax errors DEFINE (' Define_var3 ', 1 = = 1);//const Const_var3 = 1 = = 1; Const does not support comparison operators, PHP will report syntax errors $value = 3;define (' DEFINE_VAR4 ', $value);//const const_var4 = $value; Const does not support variables in the form of a value, PHP will report syntax error DEFINE (' Define_var5 ', true | | false);//const Const_var5 = True | | false; Const does not support logical operators, PHP will report syntax errors DEFINE (' define_var6 ', ' Hello '). World! '); /const const_var6 = ' Hello '. ' World! '; Const does not support string operators, PHP will report syntax errors class user{} $user = new User ();d efine (' define_var7 ', $user instanceof User);//const const_ VAR7 = $user instanceof user; Const does not support type operators, PHP will report syntax errors?>

4. Support differences for character-case sensitivity
In addition to the above 3 differences, there is a less noticeable difference. The function define () can receive the 3rd parameter, and if the argument is true, it indicates that the case of the constant name is not sensitive. Defining constants using the Const keyword does not provide similar functionality.

<?php//set code to UTF-8 to avoid Chinese garbled header (' Content-type:text/html;charset=utf-8 ' ); The 3rd parameter of//define () is true when the case is not sensitive define (' site_name ', ' Codeplayer ', true); Echo site_name; Output: Codeplayerecho site_name; Output: Codeplayerecho site_name;   Output: Codeplayerconst domain_name = ' 365mini.com '; echo domain_name;   Output: 365mini.comecho domain_name;   The PHP hint constant does not define the echo domain_name; PHP hint constants not defined?; 
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.