A constant is an identifier for a simple value. This value cannot be changed in the script.
A constant consists of English letters, underscores, and numbers, but numbers cannot appear as the first letter. (The $ modifier is not required on the constant name).
Note: Constants can be used throughout the script.
To set constants, use the define() function. The function syntax is as follows:
<code><span><span>define</span></span>(<span>string</span> constant_name, mixed value, case_sensitive <span>=</span><span>true</span>)</code>
This function has three parameters:
constant_name: required parameter, constant name, i.e. identifier.
value: required parameter, value of constant.
case_sensitive: optional parameter, specifies whether to be case sensitive, set to true to indicate insensitivity.
After a constant is defined, it defaults to a global variable and can be used anywhere in the entire running script.
The following example demonstrates the use of constants within a function. Even if the constant is defined outside the function, the constant can be used normally.
<code><span><?php</span> define(<span>"GREETING"</span>, <span>"欢迎访问 Runoob.com"</span>); <span><span>function</span><span>myTest</span><span>()</span> {</span><span>echo</span> GREETING; } myTest(); <span>// 输出 "欢迎访问 Runoob.com"</span><span>?></span></code>
The above has introduced PHP 5 constants, including related content. I hope it will be helpful to friends who are interested in PHP tutorials.