Home Backend Development PHP Tutorial Some differences between using define() and const to define constants in PHP

Some differences between using define() and const to define constants in PHP

Jun 20, 2017 am 10:15 AM
const define php use definition constant

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

1

2

3

4

<?php

define('PI', 3.14159);  //定义一个名为PI的常量

echo PI;    //输出:3.14159

?>

Copy after login

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:

1

2

3

4

5

<?php

//以下代码需在PHP 5.3.0及之后的版本中运行

const PI = 3.14159; //使用const关键字定义一个名为PI的常量

echo PI;    //输出:3.14159

?>

Copy after login

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.

1

2

3

4

5

6

7

8

9

10

11

12

13

<?php

//使用const关键字定义常量必须处于最顶端的作用区域

//也就是可以在编译时直接解析定义的地方

const DEMO = 'DEMO';

class Person{

    const MAN = '男';

    const WOMAN = '女';

}

interface USB{

    const VERSION_2 = '2.0';

    const VERSION_3 = '3.0';

}

?>

Copy after login

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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

<?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会报语法错误

?>

Copy after login

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.

1

2

3

4

5

6

7

8

9

10

11

12

13

<?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提示常量未定义

?>

Copy after login

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 of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles