Constants - PHP Manual Notes

WBOY
Release: 2016-08-08 09:29:28
Original
753 people have browsed it

Constant syntax

Constant values ​​cannot change during script execution. Constants are case-sensitive; traditionally constant identifiers are always uppercase. Once defined, a constant cannot be redefined or undefined, and its value can only be a scalar.

You can use the define() function to define constants, or you can use the const keyword to define constants outside the class definition. Do not start custom constants with double underscores, as this may conflict with magic constants.

<code>define('FOO', 'something');
const FOO = 'something';</code>
Copy after login

Constant defined using the const keyword must be in the topmost scope, because it is defined at compile time using this method. The scope of constants is global, and you can access constants anywhere in the script regardless of the scope of scope.

Magic Constant

The magic constant value can change, but it is not actually a constant.

PHP provides a large number of predefined constants to any script it runs, with 8 magic constants whose values ​​change depending on their position in the code.

<code>__LINE__  // 文件中的当前行号


__FILE__  // 文件的完整路径和文件名,绝对路径


__DIR__  // 文件所在的目录,除非根目录,否则不包括末尾斜杠,等价于dirname(__FILE__)


__FUNCTION__  // 函数名称,区分大小写


__CLASS__  // 类的名称,包括其被声明的作用区域


__TRAIT__  // Trait的名字,包括其被声明的作用区域


__METHOD__  // 类的方法名


__NAMESPACE__  // 当前命名空间的名称</code>
Copy after login

I don’t know what these magic constants do, so let’s just put them here first.

(Full text ends)

The above introduces the constants - PHP manual notes, including relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template