Deep analysis of the differences between const and define definition constants in PHP

Source: Internet
Author: User
Tags define definition

Const and define all define constants in PHP, but what are their specific differences? In fact, very simple const is used for the definition of a class member variable, once defined and cannot change its value. Define defines a global constant that can be accessed anywhere the page define cannot be defined in the class and const is possible, and an article is organized below.

As you all know, define is a constant defined, what if you define a constant in a class? Of course not with define, but with Const, as in the following example:

<? phpdefine (' php ', ' I love php '), and//usually define constants outside the class if (Defined (' php ')) {    echo ' PHP is defined! ';} class myclass{    / The value of the/constant will always remain unchanged. When defining and using constants, you do not need to use the $ symbol    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/> '; PHP 5.3.0 after Print_r (Get_defined_constants ()); All defined constants can be obtained with get_defined_constants ()

Typically define defines constants outside the class, const defines constants within the class, and const must be accessed through the class name:: Variable name. But php5.3 above support class through Const definition constants, see below, this is OK:

Const PHP_N = ' Use const defined PHP ', echo php_n;if (defined (' Php_n ')) {    echo ' php_n is defined! ';}

Constants are typically named with uppercase words and underscore intervals, constants must be defined as members of a class by const, and global constants defined with define are strongly discouraged.

On the basics of constants, here is not to say, except above, define and const other differences | Shijiazhuang Flower Company | Shijiazhuang Cemetery

1.const cannot define constants in conditional statements, but define is possible, as follows:

<?php
if (1) {
Const A = ' php ';
}
Echo A; Must be wrong

2.const takes an ordinary constant name, define can use an expression as the name

The code is as follows Copy Code

<?php
Const FOO = ' PHP ';
for ($i = 0; $i < + + + $i) {
Define (' Php_ '. $i, 1 << $i);
}

3.const can only accept static scalars, and define may take any expression.

The code is as follows Copy Code

<?php
Const PHP = 1 << 5; Error
Define (' JAVA ', 1 << 5); That's right
4.const itself is a language structure. and define is a function. So it's much faster to use the const.

Naming conventions for constants:

Constants contain numeric alphabetic characters and underscores, numbers are allowed as constant names
Use uppercase words and underline spacing, for example embed_suppress_embed_exception but don't do this embed_suppressembedexception
It is usually prefixed with the package name of the constant or the class title. For example, constants in a bug class should start with Bug_
Constants must be defined as members of a class through "const" and strongly discourage the use of global constants defined by "define"

Deep analysis of the differences between const and define definition constants in PHP

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.