Home Backend Development PHP Tutorial PHP常量详解:define和const的区别

PHP常量详解:define和const的区别

Jun 23, 2016 pm 01:23 PM

    常量是一个简单值的标识符(名字)。如同其名称所暗示的,在脚本执行期间该值不能改变(除了所谓的魔术常量,它们其实不是常量)。常量默认为大小写敏感。通常常量标识符总是大写的。    可以用 define() 函数来定义常量。在 PHP 5.3.0 以后,可以使用 const 关键字在类定义的外部定义常量,先前版本const 关键字只能在类(class)中使用。一个常量一旦被定义,就不能再改变或者取消定义。    常量只能包含标量数据(boolean,integer,float 和 string)。 可以定义 resource 常量,但应尽量避免,因为会造成不可预料的结果。    可以简单的通过指定其名字来取得常量的值,与变量不同,不应该在常量前面加上 $ 符号。如果常量名是动态的,也可以用函数constant() 来获取常量的值。用get_defined_constants() 可以获得所有已定义的常量列表。常量和变量有如下不同:&middot;常量前面没有美元符号($); &middot;常量只能用 define() 函数定义,而不能通过赋值语句; &middot;常量可以不用理会变量的作用域而在任何地方定义和访问; &middot;常量一旦定义就不能被重新定义或者取消定义; &middot;常量的值只能是标量。Example #1 定义常量```<?phpdefine("CONSTANT", "Hello world.");echo CONSTANT; // outputs "Hello world."echo Constant; // 输出 "Constant" 并发出一个提示性信息?> ```Example #2 使用关键字 const 定义常量```<?php// 以下代码在 PHP 5.3.0 后可以正常工作const CONSTANT = 'Hello World';echo CONSTANT;?>```Example #3 合法与非法的常量名```<?php// 合法的常量名define("FOO",     "something");define("FOO2",    "something else");define("FOO_BAR", "something more");// 非法的常量名define("2FOO",    "something");// 下面的定义是合法的,但应该避免这样做:(自定义常量不要以__开头)// 也许将来有一天PHP会定义一个__FOO__的魔术常量// 这样就会与你的代码相冲突define("__FOO__", "something");?>```【问】在php中定义常量时,const与define的区别? 【答】使用const使得代码简单易读,const本身就是一个语言结构,而define是一个函数。另外const在编译时要比define快很多。(1).const用于类成员变量的定义,一经定义,不可修改。define不可用于类成员变量的定义,可用于全局常量。(2).const可在类中使用,define不能。(3).const不能在条件语句中定义常量。例如:    if (...){        const FOO = 'BAR';    // 无效的invalid    }     if (...) {        define('FOO', 'BAR'); // 有效的valid    }(4).const采用一个普通的常量名称,define可以采用表达式作为名称。    const  FOO = 'BAR';       for ($i = 0; $i < 32; ++$i) {          define('BIT_' . $i, 1 << $i);      }  (5).const只能接受静态的标量,而define可以采用任何表达式。例如:    const BIT_5 = 1 << 5;    // 无效的invalid       define('BIT_5', 1 << 5); // 有效的valid  (6).const定义的常量时大小写敏感的,而define可通过第三个参数(为true表示大小写不敏感)来指定大小写是否敏感。例如:    define('FOO', 'BAR', true);       echo FOO; // BAR      echo foo; // BAR  相关函数:define — 定义一个常量说明:  bool define ( string $name , mixed $value [, bool $case_insensitive = false ]参数:  name :常量名。  value :常量的值;仅允许标量和 null。标量的类型是 integer, float,string 或者 boolean。 也能够定义常量值的类型为 resource ,但并不推荐这么做,可能会导致未知状况的发生。  case_insensitive :如果设置为 TRUE,该常量则大小写不敏感。默认是大小写敏感的。比如, CONSTANT 和 Constant 代表了不同的值。(Note: 大小写不敏感的常量以小写的方式储存。)返回值:成功时返回 TRUE, 或者在失败时返回 FALSE.constant — 返回一个常量的值说明:  mixed constant ( string $name )  通过 name 返回常量的值。当你不知道常量名,却需要获取常量的值时,constant() 就很有用了。也就是常量名储存在一个变量里,或者由函数返回常量名。该函数也适用class constants。参数:  name :常量名。返回值:  返回常量的值。如果常量未定义则返回 NULL。defined — 检查某个名称的常量是否存在说明:  bool defined ( string $name )  检查该名称的常量是否已定义。  Note: 如果你要检查一个变量是否存在,请使用 isset()。 defined() 函数仅对 constants 有效。如果你要检测一个函数是否存在,使用 function_exists()。参数:  name :常量的名称。返回值:  如果该名称的常量已定义,返回 TRUE;未定义则返回 FALSE。 get_defined_constants:Returns an associative array with the names of all the constants and their values以关联数组返回常量名和常量的值。这包括那些由扩展以及由define()函数创建的常量。 参考链接:php中定义常量define和const区别 http://www.dewen.org/q/4280
Copy after login

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 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 ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

In this article, we're going to explore the notification system in the Laravel web framework. The notification system in Laravel allows you to send notifications to users over different channels. Today, we'll discuss how you can send notifications ov

See all articles