PHP determines whether constants, variables, and functions exist _php techniques

Source: Internet
Author: User

Copy Code code as follows:

if (defined (' Const_name ')) {
Do something
}

Variable detection is the use of isset, note that the variable is not declared or declared when the assignment is Null,isset return false, such as:

Copy Code code as follows:

if (Isset ($var _name)) {
Do something
}

function detection with function_exists, note that the function name to be detected also need to use quotes, such as:

Copy Code code as follows:

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

Let's not say much more.

Copy Code code as follows:

<?php
/* To determine whether a constant exists/*
if (defined (' myconstant ')) {
Echo myconstant;
}
To determine whether a variable exists
if (Isset ($myvar)) {
echo "exists variable $myvar."
}
To determine whether a function exists
if (function_exists (' Imap_open ')) {
echo "exists function Imag_openn";
} else {
echo "function Imag_open does not exist n";
}
?>

Function_exists to determine whether a function exists

Copy Code code as follows:

<?php
if (function_exists (' Test_func ')) {
echo "function test_func exist";
} else {
echo "function Test_func does not exist";
}
?>

Filter_has_var function

The Filter_has_var () function checks for the existence of a variable of the specified input type.
If successful, returns TRUE, otherwise returns false.


Copy Code code as follows:

<?php
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.