PHP constant define and the difference between the const detailed

Source: Internet
Author: User
Tags php define scalar
in our previous article, we introduced the use of PHP define examples, I believe that we have a certain understanding of define, today we introduce you to the PHP constant define and the difference between const detailed!

A constant is a simple identifier. This value cannot be changed during script execution (in addition to the so-called magic constants, they are not constants). Constants are case sensitive by default. Usually, constant identifiers are always uppercase.

You can use the Define () function to define constants. After php5.3.0, you can use the Const keyword to define constants outside the class definition, and the previous version of the Const keyword can only be used in class. Once a constant is defined, it can no longer be changed or undefined.

Constants can only contain scalar data (Boolean, Integer, float, and string). You can define resource constants, but avoid them as much as possible, because they cause unpredictable results.

You can simply specify its name to get the value of a constant, unlike a variable, you should not precede the constant with the $ symbol. If the constant name is dynamic, you can also use the function constant () to get the value of the constant. A list of all defined constants can be obtained with get_defined_contstants ().

Constants and variables have the following differences:

1. No dollar sign ($) in front of constants

2. Constants can only be defined by the Define () function, not by an assignment statement

3. Constants can be defined and accessed anywhere, regardless of the scope of the variable

4. Once a constant is defined, it cannot be redefined or de-defined.

5, the value of the constant can only be scalar

<?php//The following code can work correctly after PHP 5.3.0 const USERNAME = ' Zhouguowei '; Echo Username;echo constant ("USERNAME"); Const ZHOUUSERNAME = ' zhouguowei2222222222 '; Define (' MyUserName ', ' zhouguowei1111111 '); echo "<pre>";p Rint_r (Get_defined_constants ());  ? >

Q: What is the difference between const and define when defining constants in PHP?

A: Using const makes the code easy to read, and const itself is a language structure, and define is a function. In addition, const is much faster to compile than define.

1, const for the definition of class member variables, once defined, can not be modified. Define cannot be used for definitions of class member variables and can be used for global constants.

2, the const can be used in the class, define cannot

3. Const can no longer define constants in conditional statements

<?phpif (...) {Const FOO = ' BAR ';    Invalid Invalid}if (...) {define (' FOO ', ' BAR ');//Valid valid}?>

4, const uses the ordinary constant name, define can use the expression as the name

<?phpconst  FOO = ' BAR '; for ($i = 0; $i <; + + $i) {define (' Bit_ '. $i, 1 << $i);}? >

5, const can only accept static scalar, and define can use any expression

<?phpconst bit_5 = 1 << 5;    Invalid Invaliddefine (' Bit_5 ', 1 << 5); The effective valid?>

6, const-defined constants are case-sensitive, and define can specify case sensitivity by the third argument (true for case insensitivity).

<?phpdefine (' FOO ', ' BAR ', true); Echo FOO; Barecho foo; Bar?>


This article through the example for everyone more intuitive understanding of PHP constant define and the difference between the const detailed, I hope that your work has helped!

Related recommendations:

PHP define in the use of detailed

PHP Define Constants Detailed

How to use the second parameter of PHP define

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: 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.