Home >Backend Development >PHP Problem >PHP determines whether a constant exists

PHP determines whether a constant exists

王林
王林Original
2019-09-25 11:54:325356browse

PHP determines whether a constant exists

php determines whether constants, variables and functions are functions

Determine whether constants are defined:

if (defined('CONST_NAME')) {
     //do something 
}

Determine whether the variable exists: isset(), note that the variable is not declared or is assigned a value of NULL when declared, isset returns both FALSE, such as:

if (isset($var_name)) {
     //do something
}

Use function_exists for function detection. Note that the function name to be detected also needs to use quotation marks, such as:

if (function_exists('fun_name')) {
  fun_name();
}

Example:

<?php 
/* 判断常量是否存在*/ 
if (defined(&#39;MYCONSTANT&#39;)) { 
echo MYCONSTANT; 
} 
//判断变量是否存在 
if (isset($myvar)) { 
echo "存在变量$myvar."; 
} 
//判断函数是否存在 
if (function_exists(&#39;imap_open&#39;)) { 
echo "存在函数imag_openn"; 
} else { 
echo "函数imag_open不存在n"; 
} 
?>

function_exists determines whether the function exists

<?php
if (function_exists(&#39;test_func&#39;)) {
    echo "函数test_func存在";
} else {
    echo "函数test_func不存在";
}
?>

filter_has_var function

filter_has_var() function checks whether the specified function exists Variable of input type. Returns true if successful, false otherwise.

<?php
if(!filter_has_var(INPUT_GET, "name"))
 {
 echo("Input type does not exist");
 }
else
 {
 echo("Input type exists");
 }
?>

Recommended tutorial: PHP video tutorial

The above is the detailed content of PHP determines whether a constant exists. 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