


As we all know, in PHP (php 4 and later), we can use the function define() to define constants, for example:
<?php define('PI', 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 = 'DEMO'; class Person{ const MAN = '男'; const WOMAN = '女'; } interface USB{ const VERSION_2 = '2.0'; const VERSION_3 = '3.0'; } ?>
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('DEFINE_VAR1', 1 << 1); //const CONST_VAR1 = (1 << 1); //const不支持位运算符,PHP会报语法错误 define('DEFINE_VAR2', 1 + 1); //const CONST_VAR2 = 1 + 1 ; //const不支持算术运算符,PHP会报语法错误 define('DEFINE_VAR3', 1 == 1); //const CONST_VAR3 = 1 == 1 ; //const不支持比较运算符,PHP会报语法错误 $value = 3; define('DEFINE_VAR4', $value); //const CONST_VAR4 = $value ; //const不支持变量形式的值,PHP会报语法错误 define('DEFINE_VAR5', true || false); //const CONST_VAR5 = true || false ; //const不支持逻辑运算符,PHP会报语法错误 define('DEFINE_VAR6', 'Hello'.' World!'); //const CONST_VAR6 = 'Hello'.' World!' ; //const不支持字符串运算符,PHP会报语法错误 class User{ }$user = new User();define('DEFINE_VAR7', $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('Content-Type:text/html;charset=utf-8'); //define()的第3个参数为true时,表示大小写不敏感 define('SITE_NAME', 'CodePlayer', true); echo SITE_NAME; //输出:CodePlayer echo site_name; //输出:CodePlayer echo SiTe_NamE; //输出:CodePlayer const DOMAIN_NAME = '365mini.com'; 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!

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

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

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

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

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

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

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

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


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

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
Chinese version, very easy to use

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version
SublimeText3 Linux latest version