Detailed explanation of the difference between define of and const defined constants in PHP

  • 2021-12-12 04:07:17
  • OfStack

This article illustrates the difference between define () and const definitions of constants in PHP. Share it for your reference, as follows:

Preface

Today, I saw another interesting article in Stackoverflow, so I picked it up after translation. The article was written by NikiC, one of the development members of PHP, and its authority is beyond doubt

Text

In PHP 5.3, there are two ways to define constants:

Use const Keyword Use define() Method

const FOO = 'BAR';
define('FOO','BAR');

The fundamental difference between these two methods is that const 1 constant is defined at code compile time, and define A constant is defined only when the code runs. This makes it possible const There are several disadvantages:

const Cannot be used in conditional statements. If you want to define a global variable, const Must be at the outermost level of the whole code:

if (...) {
  const FOO = 'BAR';  //  Invalid 
}
// but
if (...) {
  define('FOO', 'BAR'); //  Effective 
}

You can ask why I did this. The most common example is when you are checking whether a constant has been defined:


if (!defined('FOO')) {
  define('FOO', 'BAR');
}

const Can only be used to declare variables (such as numbers, strings, or true, false, null, _FILE_), while define() You can also accept expressions. However, const can also accept constant expressions after PHP 5.6:

const BIT_5 = 1 << 5;  //  In PHP5.6 Valid after, invalid before 
define('BIT_5', 1 << 5); // 1 Direct effective 

const Constants of can only be named in straightforward text, while define() Allows you to name constants with any expression. In this way, we can do the following:

for ($i = 0; $i < 32; ++$i) {
  define('BIT_' . $i, 1 << $i);
}

const The defined constants are case sensitive, but define Allows you to turn off its case sensitivity by setting its third parameter to true:

define('FOO', 'BAR', true);
echo FOO; // BAR
echo foo; // BAR

These are the points you need to pay attention to. So now let me explain the following, why not cover the above situations, which I am always used to personally using const :

const More readable and beautiful. const Default in the current namespace Define constants, and use the define You need to write down the whole namespace The full path of:

namespace A\B\C;
//  If you want to define a constant  A\B\C\FOO:
const FOO = 'BAR';
define('A\B\C\FOO', 'BAR');

Since PHP 5.6, the const Arrays can also be defined as constants. And define This 1 feature is not currently supported, but it will be implemented in PHP7:

const FOO = [1, 2, 3];  //  In PHP 5.6 Medium effective 
define('FOO', [1, 2, 3]); //  In PHP 5.6 Invalid ,  In PHP 7.0 Effective 

Because const Is executed at compile time, so it is faster than define It's almost 1:00.

Especially when using define When a large number of constants are defined, the running speed of PHP will become very slow. People have even invented such things as apc_load_constantshide To avoid this problem

And define In contrast, const doubles the efficiency of defining constants (the difference is even greater on development machines equipped with XDebug). However, in terms of query time, there is no difference between the two (because both of them use the same query table)

The last point to note is that, const Can be found in class And interface Which is used, and define You can't do this 1 point:


class Foo {
  const BAR = 2; //  Effective 
}
class Baz {
  define('QUX', 2); //  Invalid 
}

Summarize

Unless you need to use expressions or define constants in conditional statements, you'd better use them just for the sake of simple readability of the code const !

For more readers interested in PHP related contents, please check the special topics of this site: "Summary of Common Functions and Skills of php", "Summary of Usage of php String (string)", "Encyclopedia of Operation Skills of PHP Array (Array)", "Introduction to Basic Grammar of PHP", "Introduction to Database Operation of php+mysql" and "Summary of Operation Skills of Common Database of php"

I hope this article is helpful to everyone's PHP programming.


Related articles: