In PHP, variable
refers to the numerical value used in the program that can be changed. The opposite is constant
, constant Once the value
is defined, it cannot be changed anywhere else in the script.
Syntax:
define ( string $name , mixed $value , bool $case_insensitive = false )
$name: Constant name.
$value: The value of a constant; in PHP 5, value must be a scalar
(int, float, string, boolean, null) in PHP 7 A value of array
is allowed.
$case_insensitive: If set to true
, this constant is case-insensitive. The default is case-sensitive. PHP 7.3.0
The definition of case-insensitive constants has been abandoned.
Return value: Returns true
on success, or false
on failure.
Usage example:
a.Case sensitive
<?php define("OK", "Hello world."); echo OK; echo Ok; ?>
输出: php.cn Warning: Use of undefined constant Ok - assumed 'Ok' (this will throw an Error in a future version of PHP)
b.Not size sensitive Write
<?php define("OK", "php.cn", true); echo OK."<br>"; echo Ok; ?>
输出: php.cn php.cn
c. Allowed to be array
<?php define('People', array( 'man', 'woman', 'strick' )); echo People[1]; ?>
输出:woman
Recommended: 《2021 PHP Interview Questions Summary (Collection)》《php video tutorial》
The above is the detailed content of How to use define to define constants in PHP. For more information, please follow other related articles on the PHP Chinese website!