Deep analysis of PHP const and define usage differences

Source: Internet
Author: User
Tags constant

Note: Define cannot be defined in a class, and the const must be defined in a class, and the const must be accessed by the class name:: Variable name


1. Const is used for class member variable definition once defined and cannot change its value. Define defines global constants that can be accessed anywhere.
2, define can not be defined in the class and Const.
3. Const cannot define constants in conditional statements

4, the const use a common constant name, define can use an expression as the name.
5, const can only accept static scalar, and define can take any expression.
6. Const is always case sensitive, but define () can define a case insensitive constant with a third argument
7. Using the const simple and easy to read, which is itself a language structure, and define is a method, with the const definition at compile time much faster than define.

Define is defined as constants, what if constants are defined in the class? Certainly cannot use define, but uses the const, the following example:

The code is as follows Copy Code

<?php
Outside of the class usually this defines constants
Define ("PHP", "111cn.net");
Class MyClass
{
The value of the constant will always remain 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 get all the defined constants 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 the support class to define constants by const, see below, this is OK:

The code is as follows Copy Code


<?php
//@blog Const A = "abcdef";
echo A;
?>

The basics of constants are not mentioned here, except for the other differences between define and const (excerpt from the network):

1.const cannot define constants in a conditional statement, but define is OK, as follows:

The code is as follows Copy Code
<?php
if (1) {
Const A = ' Java ';
}
echo A; Must be wrong
?>

2.const uses 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 (' PHP ', 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.

Two sync: Both are not assignable.

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.