


The difference between php const and define, phpconstdefine_PHP tutorial
The difference between php const and define, phpconstdefine
Original address: http://www.manongjc.com/article/491.html
const is used for the definition of class member constants, which cannot be changed after definition. With define, we define global constants, so that we can access them in other places but cannot change them. We will list some details below. Come out
Note: define cannot be defined in a class, but const must be defined in a class, and const must be accessed through class name::variable name
1. Const is used to define class member variables. Once defined, its value cannot be changed. define defines global constants that can be accessed anywhere.
2. define cannot be defined in a class but const can.
3. Const cannot define constants in conditional statements
4. const uses an ordinary constant name, and define can use an expression as the name.
5. const can only accept static scalars, while define can use any expression.
6. const is always case-sensitive, however define() can use the third parameter to define case-insensitive constants
7. Using const is simple and easy to read. It itself is a language structure, while define It is a method, using const definition is much faster than define at compile time.
define defines constants. What if we define constants in a class? Of course, you cannot use define, but use const, as in the following example:
<?<span>php </span><span>//</span><span>在类外面通常这样定义常量</span> <span>define</span>("PHP","111cn.net"<span>); </span><span>class</span><span> MyClass { </span><span>//</span><span>常量的值将始终保持不变。在定义和使用常量的时候不需要使用$符号</span> <span>const</span> <span>constant</span> = 'constant value'<span>; </span><span>function</span><span> showConstant() { </span><span>echo</span> self::<span>constant</span> . "<br>"<span>; } } </span><span>echo</span> MyClass::<span>constant</span> . "<br>"<span>; </span><span>$classname</span> = "MyClass"<span>; </span><span>echo</span> <span>$classname</span>::<span>constant</span> . "<br>"; <span>//</span><span> PHP 5.3.0之后</span> <span>$class</span> = <span>new</span><span> MyClass(); </span><span>$class</span>-><span>showConstant(); </span><span>echo</span> <span>$class</span>::<span>constant</span>."<br>"; <span>//</span><span> PHP 5.3.0之后 //print_r(get_defined_constants()); //可以用get_defined_constants()获取所有定义的常量</span> ?>
Generally define defines constants outside the class, const defines constants within the class, and const must be accessed through class name::variable name. However, php5.3 and above support defining constants through const outside the class, as shown below, this is OK:
<?<span>php </span><span>const</span> a = "abcdef"<span>; </span><span>echo</span><span> a; </span>?>
I won’t go into the basic knowledge about constants here. In addition to the above, other differences between define and const (taken from the Internet):
1.const cannot define constants in conditional statements, but define is possible, as follows:
<?<span>php </span><span>if</span>(1<span>){ </span><span>const</span> a = 'java'<span>; } </span><span>echo</span> a; <span>//</span><span>必错</span> ?>
2.const uses an ordinary constant name, define can use an expression as the name
<?<span>php </span><span>const</span> FOO = 'PHP'<span>; </span><span>for</span> (<span>$i</span> = 0; <span>$i</span> < 32; ++<span>$i</span><span>) { </span><span>define</span>('PHP_' . <span>$i</span>, 1 << <span>$i</span><span>); } </span>?>
3.const can only accept static scalars, while define can take any expression.
<?<span>php </span><span>const</span> PHP = 1 << 5; <span>//</span><span> 错误</span> <span>define</span>('PHP', 1 << 5); <span>//</span><span> 正确 </span> ?>
4.const itself is a language structure. And define is a function. So using const is much faster.
Two common synchronizations: both cannot be reassigned.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Detailed explanation and code examples of const in C In C language, the const keyword is used to define constants, which means that the value of the variable cannot be modified during program execution. The const keyword can be used to modify variables, function parameters, and function return values. This article will provide a detailed analysis of the use of the const keyword in C language and provide specific code examples. const modified variable When const is used to modify a variable, it means that the variable is a read-only variable and cannot be modified once it is assigned a value. For example: constint

This article brings you relevant knowledge about JavaScript. It mainly introduces the differences between var, let and const, as well as the relationship between ECMAScript and JavaScript. Interested friends can take a look at it. I hope Helpful to everyone.

const is a keyword that can be used to declare constants, const modifiers in function parameters, const modified function return values, and const modified pointers. Detailed introduction: 1. Declare constants. The const keyword can be used to declare constants. The value of the constant cannot be modified during the running of the program. The constant can be a basic data type, such as integer, floating point number, character, etc., or a custom data type; 2. The const modifier in the function parameters. The const keyword can be used in the parameters of the function, indicating that the parameter cannot be modified inside the function, etc.

Audio output and input require specific drivers and services to work as expected on Windows 11. These sometimes end up running into errors in the background, causing audio issues like no audio output, missing audio devices, distorted audio, etc. How to Fix Audio Service Not Responding on Windows 11 We recommend you to start with the fixes mentioned below and work your way through the list until you manage to resolve your issue. The audio service may become unresponsive for a number of reasons on Windows 11. This list will help you verify and fix most issues that prevent audio services from responding on Windows 11. Please follow the relevant sections below to help you through the process. Method 1: Restart the audio service. You may encounter

Correct usage of the const keyword in C++: Using const to modify a function means that the function will not modify the parameters or class members passed in. Using const to declare a function pointer means that the pointer points to a constant function.

For C++ programmers, syntax errors are one of the most common problems. One of the common mistakes is that const objects must be initialized at definition time. If you encounter this situation, how should you deal with it? First, we need to understand what a const object is. The const keyword is a special type qualifier in C++ that specifies that the value of a variable cannot be changed during the execution of the program. Such variables are called "constants". If you define a const object without initializing it, you will encounter the above error. This is

As a strongly typed language, C++ needs to consider many details when performing type conversion. A common problem is that const objects cannot be converted into non-const objects. This problem is more common when pointers and references are involved. Next, we will detail the causes and solutions to this problem. The cause of the problem is that the const keyword in C++ is used to define constants. Once a constant is defined, it cannot be modified. When we convert a const object to a non-const object, we are actually trying to modify a

In C++, const pointers point to unmodifiable data, while immutable objects have the characteristics that they cannot be modified. The main advantages are: const pointers: prevent the data pointed to from being accidentally written and ensure data integrity. Immutable objects: By making class member variables const, objects that cannot be modified are created to ensure data security.
