Table of Contents
PHP_Const,constphp
Home php教程 php手册 PHP_Const,constphp

PHP_Const,constphp

Jun 13, 2016 am 08:46 AM
const

PHP_Const,constphp

PHP_Const

常量 规则:
1 总是大写
2 A-Z 及 从127~255的ASCII字符
3 全局范围
4 用define函数定义
5 只能包含标量数据 如Boolean integer float string
6 前面不可加美元符

PHP自带常量=特殊常量
不区分大小写
_LINE_        文件中的当前行号
_FILE_        文件的完整路径+文件名
_FUNCTION_    函数名称
_CLASS_        类名称
_METHOD_    类的方法名

_LINE_
php脚本行数 如果引用文件 则在引用文件内的该常量为被引用文件的行
而非引用文件的行 即向下传递

_FILE_
原理同上

 

define部分:
  宏不仅可以用来代替常数值,还可以用来代替表达式,甚至是代码段。

  (宏的功能很强大,但也容易出错,所以其利弊大小颇有争议。)


宏的语法为:
#define 宏名称 宏值
作为一种建议和一种广大程序员共同的习惯,宏名称经常使用全部大写的字母。


利用宏的优点:
  1)让代码更简洁明了
    当然,这有赖于你为宏取一个适当的名字。一般来说,宏的名字更要注重有明确直观的意义,有时宁可让它长点。
  2)方便代码维护
    对宏的处理,在编译过程中称为“预处理”。

    也就是说在正式编译前,编译器必须先将代码出现的宏,用其相应的宏值替换,这个过程有点像你我在文字处理软件中的查找替换。所以在代码中使用宏表达常数,归根结底还是使用了立即数,并没有明确指定这个量的类型。

const部分
  常量定义的格式为:
    const 数据类型 常量名 = 常量值;

    常量必须一开始就指定一个值,然后,在以后的代码中,我们不允许改变此常量的值。

两者之间的区别:
  1 内存空间的分配上。

    define进行宏定义的时候,不会分配内存空间,编译时会在main函数里进行替换,只是单纯的替换,不会进行任何检查,

    比如类型,语句结构等,即宏定义常量只是纯粹的置放关系,如#define null 0;

    编译器在遇到null时总是用0代替null它没有数据类型(还有疑问请找C语言书籍看预处理部分或者看MSDN.

    const定义的常量 具有数据类型,

    定义数据类型的常量便于编译器进行数据检查,使程序可能出现错误进行排查,

    所以const与define之间的区别在于

      const定义常量排除了程序之间的不安全性.

      define定义全局常量,在任何地方都可以访问

const用于类成员变量定义,只能用类名访问不能更改要是初学这样先理解着别太钻牛角尖就行

 

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 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)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
Deep understanding of const in C language Deep understanding of const in C language Feb 18, 2024 pm 12:56 PM

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

18 Ways to Fix Audio Service Not Responding Issue on Windows 11 18 Ways to Fix Audio Service Not Responding Issue on Windows 11 Jun 05, 2023 pm 10:23 PM

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

C++ syntax error: const objects must be initialized when defined, how to deal with it? C++ syntax error: const objects must be initialized when defined, how to deal with it? Aug 22, 2023 am 09:13 AM

For C++ programmers, syntax errors are one of the most common problems. One of the common mistakes is that const objects must be initialized at definition time. If you encounter this situation, how should you deal with it? First, we need to understand what a const object is. The const keyword is a special type qualifier in C++ that specifies that the value of a variable cannot be changed during the execution of the program. Such variables are called "constants". If you define a const object without initializing it, you will encounter the above error. This is

What are the correct uses of the const keyword in C++ functions? What are the correct uses of the const keyword in C++ functions? Apr 11, 2024 pm 02:36 PM

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.

How to use const in c language How to use const in c language Sep 20, 2023 pm 01:34 PM

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.

Let's talk about the differences between var, let and const (code example) Let's talk about the differences between var, let and const (code example) Jan 06, 2023 pm 04:25 PM

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.

C++ error: Cannot convert const object to non-const object, how to solve it? C++ error: Cannot convert const object to non-const object, how to solve it? Aug 22, 2023 am 08:33 AM

As a strongly typed language, C++ needs to consider many details when performing type conversion. A common problem is that const objects cannot be converted into non-const objects. This problem is more common when pointers and references are involved. Next, we will detail the causes and solutions to this problem. The cause of the problem is that the const keyword in C++ is used to define constants. Once a constant is defined, it cannot be modified. When we convert a const object to a non-const object, we are actually trying to modify a

C++ syntax error: const-modified member functions must declare const members, how to deal with it? C++ syntax error: const-modified member functions must declare const members, how to deal with it? Aug 22, 2023 pm 01:51 PM

C++ syntax error: const-modified member functions must declare const members, how to deal with it? In the C++ language, const is a very important keyword, which is used to modify certain variables, pointers, member functions, etc. For member functions, if it is modified with the const keyword, the value of the member variable cannot be modified inside the function body. However, if we do not add the const keyword in both the function declaration and definition, we will encounter a compilation error "The const-modified member function must be declared

See all articles