Constants are similar to variables, but once a constant is defined, it cannot be changed or undefined.
PHP Constants
Constants are identifiers (names) of a single value. This value cannot be changed in script.
Valid constant names start with a character or an underscore (no $ sign in front of the constant name).
Note: Unlike variables, constants are automatically global throughout the entire script.
Set PHP constants
To set a constant, use the define() function - it takes three parameters:
<code>首个参数定义常量的名称 第二个参数定义常量的值 可选的第三个参数规定常量名是否对大小写敏感。默认是 false,即敏感。 </code>
The following example creates a case-sensitive constant with the value "Welcome to W3School.com. cn!":
<code><span><span><?php</span><span>// 定义对大小写敏感的常量</span> define(<span>"GREETING"</span>, <span>"Welcome to W3School.com.cn!"</span>); <span>echo</span> GREETING; <span>echo</span><span>"<br>"</span>; <span>// 不会输出常量的值</span><span>echo</span> greeting; <span>?></span></span></code>
The following example creates a case-insensitive constant with the value "Welcome to W3School.com.cn!":
<code><span><span><span><?php</span><span>// 定义对大小写不敏感的常量</span> define(<span>"GREETING"</span>, <span>"Welcome to W3School.com.cn!"</span>, <span>true</span>); <span>echo</span> GREETING; <span>echo</span><span>"<br>"</span>; <span>// 会输出常量的值</span><span>echo</span> greeting; <span>?></span></span></span></code>
Copyright statement: This article is an original article by the blogger and has not been authorized by the author. Reprinting is not allowed with the permission of the blogger.
The above has introduced 10 PHP constants, including related content. I hope it will be helpful to friends who are interested in PHP tutorials.