Php to determine whether constants, variables, and functions exist _ PHP Tutorial

Source: Internet
Author: User
Php checks whether constants, variables, and functions exist. In php development, we often choose to provide predefined variables, constants, or functions to determine if they exist, next I will introduce some common judgment constants, variables, and functions to check whether the application exists. in php development, we often meet to provide pre-defined judgment variables, constants, or functions, next I will introduce some common examples of checking whether constants, variables, and functions exist in applications.


Defined is used for constant detection, and define is used for defining constants. Note that the constants to be detected must be enclosed in quotation marks (single and double quotation marks), for example:

The code is as follows:

If (defined ('const _ name ')){
// Do something
}

Isset is used for variable Detection. Note that if the variable is not declared or declared, the value is NULL, and isset returns FALSE, for example:

The code is as follows:

If (isset ($ var_name )){
// Do something
}

Function_exists is used for function Detection. you must use quotation marks for the function name to be detected, for example:

If (function_exists ('fun _ name ')){
Fun_name ();
}

Let's look at an instance.

The code is as follows:


/* Determine whether a constant exists */
If (defined ('myconstant ')){
Echo MYCONSTANT;
}
// Determine whether a variable exists
If (isset ($ myvar )){
Echo "variable $ myvar .";
}
// Determine whether a function exists
If (function_exists ('imap _ open ')){
Echo "the imag_openn function exists ";
} Else {
Echo "the imag_open function does not exist n ";
}
?>


Function_exists

The code is as follows:

If (function_exists ('test _ func ')){
Echo "the test_func function exists ";
} Else {
Echo "the test_func function does not exist ";
}
?>

Filter_has_var function

The filter_has_var () function checks whether a variable of the specified input type exists.

If yes, true is returned. otherwise, false is returned.

The code is as follows:
If (! Filter_has_var (INPUT_GET, "name "))
{
Echo ("Input type does not exist ");
}
Else
{
Echo ("Input type exists ");
}
?>

Output is. Input type exists

...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.