defined() is a built-in function in PHP, used to check whether a constant exists, that is, whether a constant is defined; the syntax format is "defined(name)", and the parameter name is the name of the constant to be checked. Returns TRUE if the constant exists, FALSE otherwise.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php defined() function
defined() function checks whether a constant exists.
Syntax
defined(name)
name: Specifies the name of the constant to be checked and cannot be omitted.
Return value: TRUE if the constant exists, otherwise FALSE.
Note: This feature is available in PHP 4.0.0 and higher.
Example 1:
<?php define("constant_key", "value for the constant key"); echo defined("constant_key"); ?>
Output:
Example 2: Check condition after defining constant Whether it is established.
<?php define("constant_key", "value for the constant key"); if(defined("constant_key")){ echo "constant_key is defined"; }else{ echo "constant_key is not defined"; } ?>
Output:
Example 3: Checking whether a condition is met without defining a constant.
<?php //define("constant_key", "value for the constant key"); if(defined("constant_key")){ echo "constant_key is defined"; }else{ echo "constant_key is not defined"; } ?>
Output:
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to use defined() function in php. For more information, please follow other related articles on the PHP Chinese website!