Home > Article > Backend Development > How to define a constant in php
In PHP, use define("CONSTANT_NAME", value) to define a constant, and its value will not change during execution. Constant names follow the variable naming rules, and the value can be of any type.
Define constants in PHP
In PHP, a constant is a special variable whose value is Remains unchanged during script execution. To define a constant, you can use the following syntax:
<code class="php">define("CONSTANT_NAME", value);</code>
where:
Example:
<code class="php">define("MY_CONSTANT", "Hello World");</code>
After defining a constant, you can use it like an ordinary variable:
<code class="php">echo MY_CONSTANT; // 输出 "Hello World"</code>
Notes:
const
keyword instead of the define
function to define constants, but the syntax is the same. The above is the detailed content of How to define a constant in php. For more information, please follow other related articles on the PHP Chinese website!