How to use defined() function in php

青灯夜游
Release: 2023-03-09 09:52:02
Original
3563 people have browsed it

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

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"); 
?>
Copy after login

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"; 
} 
?>
Copy after login

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"; 
} 
?>
Copy after login

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!

Related labels:
php
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