In-depth analysis of the differences between const and define defined constants in php

Source: Internet
Author: User
Tags constant

As we all know, define defines constants. What if we define constants in a class? Of course, define cannot be used, but const can be used, for example:

The code is as follows: Copy code

<? Php
Define ('php', 'I love php'); // constants are usually defined out of the class.
If (defined ('php ')){
Echo 'php is defined! ';
}
 
Class MyClass
{
// The constant value remains unchanged. You do not need to use the $ symbol when defining and using constants.
Const CONSTANT = 'constant value ';
 
Function showConstant (){
Echo self: CONSTANT. '<br/> ';
    }
}
 
Echo MyClass: CONSTANT. '<br/> ';
 
$ Classname = 'myclass ';
Echo $ classname: CONSTANT. '<br/>'; // after PHP 5.3.0
 
$ Class = new MyClass ();
$ Class-> showConstant ();
Echo $ class: CONSTANT. '<br/>'; // after PHP 5.3.0
 
Print_r (get_defined_constants (); // you can use get_defined_constants () to obtain all defined constants.

Generally, define defines constants outside the class, const defines constants within the class, and const must be accessed by class name: variable name. However, php5.3 and above support defining constants through const outside the class, as shown below, it is OK:

 

The code is as follows: Copy code
Const PHP_N = 'use const defined php ';
Echo PHP_N;
If (defined ('php _ n ')){
Echo 'php _ N is defined! ';
}

Constants are generally named at an interval between upper-case words and underscores. Constants must be defined as members of the class through const. We strongly recommend that you do not use global constants defined by define.

I will not talk about the basic knowledge of constants here, except for the above, the difference between define and const

1. const cannot define constants in condition statements, but define is acceptable, as follows:

The code is as follows: Copy code
<? Php
If (1 ){
Const A = 'php ';
}
Echo A; // required

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

The code is as follows: Copy code

 

<? Php
Const FOO = 'php ';
For ($ I = 0; $ I <32; ++ $ I ){
Define ('php _ '. $ I, 1 <$ I );
}

3. const can only accept static scalar, while define can use any expression.

The code is as follows: Copy code

<? Php
Const PHP = 1 <5; // error
Define ('Java', 1 <5); // correct
4. const itself is a language structure. Define is a function. Therefore, using const is much faster.

 

Constant naming rules:

A constant contains numbers, letters, and underscores. A number can be a constant name.
The interval between upper-case words and underscores is used. For example, EMBED_SUPPRESS_EMBED_EXCEPTION can be used, but EMBED_SUPPRESSEMBEDEXCEPTION cannot be used.
It usually uses the package name or class name where the constant is located as the prefix. For example, the constant in the Bug class should start with BUG _.
Constants must be defined as class members through "const". We strongly do not encourage the use of global constants defined by "define".

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.