PHP determines whether constants, variables and functions exist example code

怪我咯
Release: 2023-03-13 09:20:01
Original
1207 people have browsed it

If you understand the above sentence, then the rest is nonsense. PHP Manual is still very comprehensive. One sentence solves all the problems in my title.

The code is as follows:

if (defined('CONST_NAME')) {
    //do something 
}
Copy after login

Variable is detected using isset. Note that the variable is not declared or is assigned a value of NULL when declared, and isset returns FALSE. , such as:

Copy code The code is as follows:

if (isset($var_name)) {
    //do something
}
Copy after login

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

The code is as follows:

if (function_exists('fun_name')) {
 fun_name();
}
Copy after login

Without further ado, let’s look at an example

The code is as follows:

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

function_exists determines whether the function exists

The code is as follows:

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

filter_has_var function

filter_has_var() function checks whether there is a variable of the specified input type.
If successful, return true, otherwise return false.

The code is as follows:

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

The output is. Input type exists

The above is the detailed content of PHP determines whether constants, variables and functions exist example code. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!