Home >Backend Development >PHP Problem >How to use defined() function in php

How to use defined() function in php

青灯夜游
青灯夜游Original
2021-04-01 18:23:593590browse

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.

How to use defined() function in php

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:

How to use defined() function in php

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:

How to use defined() function in php

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:

How to use defined() function in php

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!

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