


Detailed explanation of the difference between const and define in PHP
##Available when defining constants in PHP What is the difference between const and define?
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 must be defined in a class, and variables defined by const must be accessed through class name::variable name. 3. Const constants cannot be defined in conditional statements. 4. const uses an ordinary constant name (static scalar), and define can use any expression as the name. 5. const is always case-sensitive, but define() can define case-insensitive constants through the third parameter. 6. Using const is simple and easy to read. It is a language structure in itself, and define is a method. Using const to define is much faster than define at compile time. If you define a constant in a class, you cannot use define, but use const, as in the following example:PHP Tutorial"
<?php //在类外面通常这样定义常量 define("PHP","111cn.net"); class MyClass { //常量的值将始终保持不变。在定义和使用常量的时候不需要使用$符号 const constant = 'constant value'; function showConstant() { echo self::constant . "<br>"; } } echo MyClass::constant . "<br>"; $classname = "MyClass"; echo $classname::constant . "<br>"; // PHP 5.3.0之后 $class = new MyClass(); $class->showConstant(); echo $class::constant."<br>"; // PHP 5.3.0之后 //print_r(get_defined_constants()); //可以用get_defined_constants()获取所有定义的常量 ?>
<?php const a = "abcdef"; echo a; ?>
I won’t go into the basic knowledge about constants here. In addition to the above, define and const are other things. Difference (from the Internet):
1.const cannot define constants in conditional statements, but define is possible, as follows:<?php if(1){ const a = 'java'; } echo a; //必错 ?>
<?phpconst FOO = 'PHP'; for ($i = 0; $i < 32; ++$i) { define('PHP_' . $i, 1 << $i); } ?>
<?php const PHP = 1 << 5; // 错误 define('PHP', 1 << 5); // 正确 ?>
The following content is excerpted from Rotted_Pencil's blog post: The difference between defining constants in PHP, define() vs. const
Preface Read it again on Stackoverflow today I came across a very interesting article, so I translated it and picked it up. The article was written by NikiC, one of the PHP development members, and its authority is unquestionableText In PHP5.3, there are two ways to define constants:1. Use the const keyword 2. Use the define() methodconst FOO = ‘BAR’; define(‘FOO’,’BAR’);
if (...) { const FOO = 'BAR'; // 无效的 } // but if (...) { define('FOO', 'BAR'); // 有效的 }
if (!defined('FOO')) { define('FOO', 'BAR'); }
const BIT_5 = 1 << 5; // 在PHP5.6之后有效,之前无效 define('BIT_5', 1 << 5); // 一直有效
for ($i = 0; $i < 32; ++$i) { define('BIT_' . $i, 1 << $i); }
define('FOO', 'BAR', true); echo FOO; // BAR echo foo; // BAR
namespace A\B\C; // 如果要定义常量 A\B\C\FOO: const FOO = ‘BAR’; define(‘A\B\C\FOO’, ‘BAR’);
const FOO = [1, 2, 3]; // 在PHP 5.6中有效 define(‘FOO’, [1, 2, 3]); // 在PHP 5.6无效, 在PHP 7.0有效
class Foo { const BAR = 2; // 有效 } class Baz { define('QUX', 2); // 无效 }
Unless you need to use expressions or define constants in conditional statements, otherwise you'd better use const just for the simple readability of the code!
For more PHP related knowledge, please visit PHP Chinese website!
The above is the detailed content of Detailed explanation of the difference between const and define in PHP. For more information, please follow other related articles on the PHP Chinese website!

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



define defines a multi-line macro by using `\` to divide `do { \ printf("%d\n", x); \ } while (0)` into multiple lines for definition. In a macro definition, the backslash `\` must be the last character of the macro definition and cannot be followed by spaces or comments. When using `\` for line continuation, be careful to keep the code readable and make sure there is a `\` at the end of each line.

The importance and role of the define function in PHP 1. Basic introduction to the define function In PHP, the define function is a key function used to define constants. Constants will not change their values during the running of the program. Constants defined using the define function can be accessed throughout the script and are global. 2. The syntax of define function The basic syntax of define function is as follows: define("constant name","constant value&qu

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

defineConditional compilation can be achieved using the `#ifdef`, `#ifndef`, `#if`, `#elif`, `#else` and `#endif` preprocessing directives.

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.

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.

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.
