Home  >  Article  >  Backend Development  >  PHP 5 constants

PHP 5 constants

WBOY
WBOYOriginal
2016-07-29 08:58:20912browse

  • PHP Constants

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.

  • Set PHP constants

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.

  • Constants are global

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>
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above has introduced PHP 5 constants, including related content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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