search
HomeBackend DevelopmentPHP TutorialSome differences between using define() and const to define constants in PHP
Some differences between using define() and const to define constants in PHPJun 20, 2017 am 10:15 AM
constdefinephpusedefinitionconstant

As we all know, in PHP (php 4 and later), we can use the function define() to define constants, for example:

<?php
define(&#39;PI&#39;, 3.14159);  //定义一个名为PI的常量
echo PI;    //输出:3.14159
?>

However, after PHP 5.3.0, in addition to using the function define() In addition, we can also use the PHP keyword const to define constants.

For example:

<?php
//以下代码需在PHP 5.3.0及之后的版本中运行
const PI = 3.14159; //使用const关键字定义一个名为PI的常量
echo PI;    //输出:3.14159
?>

Although both of the above methods can define constants, what are the differences between them. Let's explain one by one the difference between the define() function and the const keyword in PHP to define constants:

1. Version differences
First of all, there is no doubt that the difference between the two ways of defining constants There are version differences. The function define() can be used in both PHP4 and PHP5, and the keyword const can only be used in PHP 5.3.0 and later versions.

2. The difference in definition location
Since the constants defined by the function define() are defined when the define() function is executed, functions can be used within functions, loops, if statements, etc. Use the define() function to define constants wherever called. Unlike define(), since the constant defined by the const keyword is defined at compile time, the constant defined by the const keyword must be in the topmost scope. This also means that const cannot be used to define constants within functions, loops, and if statements.

<?php
//使用const关键字定义常量必须处于最顶端的作用区域
//也就是可以在编译时直接解析定义的地方
const DEMO = &#39;DEMO&#39;;
class Person{
    const MAN = &#39;男&#39;;
    const WOMAN = &#39;女&#39;;
}
interface USB{
    const VERSION_2 = &#39;2.0&#39;;
    const VERSION_3 = &#39;3.0&#39;;
}
?>

3. Differences in value expression support
Although the constant values ​​defined by the keywords const and define() can only be null or scalar data (boolean, integer, float and string types) And the resource type (it is not recommended to define constants of the resource type, otherwise unpredictable results may occur). However, since the constant defined by the keyword const is defined at compile time, the expression of the constant value defined by the const keyword does not support arithmetic operators, bit operators, comparison operators and other operators, which can be used directly when defining constants in the define() function.

<?php
define(&#39;DEFINE_VAR1&#39;, 1 << 1);
//const CONST_VAR1 = (1 << 1); //const不支持位运算符,PHP会报语法错误
define(&#39;DEFINE_VAR2&#39;, 1 + 1);
//const CONST_VAR2 = 1 + 1 ; //const不支持算术运算符,PHP会报语法错误
define(&#39;DEFINE_VAR3&#39;, 1 == 1);
//const CONST_VAR3 = 1 == 1 ; //const不支持比较运算符,PHP会报语法错误
$value = 3;
define(&#39;DEFINE_VAR4&#39;, $value);
//const CONST_VAR4 = $value ; //const不支持变量形式的值,PHP会报语法错误
define(&#39;DEFINE_VAR5&#39;, true || false);
//const CONST_VAR5 = true || false ; //const不支持逻辑运算符,PHP会报语法错误
define(&#39;DEFINE_VAR6&#39;, &#39;Hello&#39;.&#39; World!&#39;);
//const CONST_VAR6 = &#39;Hello&#39;.&#39; World!&#39; ; //const不支持字符串运算符,PHP会报语法错误
class User{ 
}$user = new User();define(&#39;DEFINE_VAR7&#39;, $user instanceof User);
//const CONST_VAR7 = $user instanceof User ; //const不支持类型运算符,PHP会报语法错误
?>

4. Differences in support for character case sensitivity
In addition to the above three differences, there is also a less eye-catching difference. The function define() can receive the third parameter. If this parameter is true, it means that the constant name is not case-sensitive. However, using the const keyword to define constants does not provide similar functionality.

<?php
//设置编码为UTF-8,以避免中文乱码
header(&#39;Content-Type:text/html;charset=utf-8&#39;);
//define()的第3个参数为true时,表示大小写不敏感
define(&#39;SITE_NAME&#39;, &#39;CodePlayer&#39;, true);
echo SITE_NAME; //输出:CodePlayer
echo site_name; //输出:CodePlayer
echo SiTe_NamE; //输出:CodePlayer
const DOMAIN_NAME = &#39;365mini.com&#39;;
echo DOMAIN_NAME;   //输出:365mini.com
echo domain_name;   //PHP提示常量未定义
echo DomaIN_nAMe;   //PHP提示常量未定义
?>

The above is the detailed content of Some differences between using define() and const to define constants in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

define怎么定义多行宏define怎么定义多行宏Oct 11, 2023 pm 01:24 PM

define定义多行宏可以通过使用 `\` 将 `do { \ printf("%d\n", x); \ } while (0)` 分成了多行进行定义。在宏定义中,反斜杠 `\` 必须是宏定义的最后一个字符,且不能有空格或注释跟随。使用 `\` 进行续行时,注意保持代码的可读性,并确保每个行末都有 `\`。

探究PHP中define函数的重要性与作用探究PHP中define函数的重要性与作用Mar 19, 2024 pm 12:12 PM

PHP中define函数的重要性与作用1.define函数的基本介绍在PHP中,define函数是用来定义常量的关键函数,常量在程序运行过程中不会改变其值。利用define函数定义的常量,在整个脚本中均可被访问,具有全局性。2.define函数的语法define函数的基本语法如下:define("常量名称","常量值&qu

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

深入理解C语言中的const深入理解C语言中的constFeb 18, 2024 pm 12:56 PM

C中const的详解及代码示例在C语言中,const关键字用于定义常量,表示该变量的值在程序执行过程中不能被修改。const关键字可以用于修饰变量、函数参数以及函数返回值。本文将对C语言中const关键字的使用进行详细解析,并提供具体的代码示例。const修饰变量当const用于修饰变量时,表示该变量为只读变量,一旦赋值就不能再修改。例如:constint

php怎么将url的参数转化成数组php怎么将url的参数转化成数组Apr 21, 2022 pm 08:50 PM

转化方法:1、使用“mb_substr($url,stripos($url,"?")+1)”获取url的参数部分;2、使用“parse_str("参数部分",$arr)”将参数解析到变量中,并传入指定数组中,变量名转为键名,变量值转为键值。

php怎么去除首位数字php怎么去除首位数字Apr 20, 2022 pm 03:23 PM

去除方法:1、使用substr_replace()函数将首位数字替换为空字符串即可,语法“substr_replace($num,"",0,1)”;2、用substr截取从第二位数字开始的全部字符即可,语法“substr($num,1)”。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version