Home > Backend Development > PHP Tutorial > 10 PHP constants

10 PHP constants

WBOY
Release: 2016-08-08 09:20:00
Original
886 people have browsed it

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>
Copy after login

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>
Copy after login

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>
Copy after login

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.

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