Php to determine whether a variable constant exists _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Php checks whether a variable constant exists. In php, we have common variables and constants for variable types. next I will introduce how to judge whether constants and variables are stored in php, for more information about variable types in php, see common variables and constants. next I will introduce how to judge whether constants and variables are stored in php, for more information, see.

The defined () function checks whether a constant exists.

If a constant exists, true is returned; otherwise, false is returned.

The code is as follows:


If (defined ('myconstant ')){
Echo "constant MYCONSTANT exists ";
} Else {
Echo "constant MYCONSTANT does not exist ";
}
Echo"
";


The isset function checks whether variables are set.

1. if the variable does not exist, FALSE is returned.
2. if the variable exists and its value is NULL, FALSE is also returned.
3. if the variable exists and the value is not NULL, true is returned.
4. when multiple variables are checked at the same time, TRUE is returned only when each individual item meets the previous requirement. Otherwise, the result is

The code is as follows:

$ Var = '';

If (isset ($ var )){
Print "This var is set so I will print .";
}

// In the following example, we will use the var_dump function to output the returned value of isset.

$ A = "test ";
$ B = "anothertest ";

Var_dump (isset ($ a); // TRUE
Var_dump (isset ($ a, $ B); // TRUE

Unset ($ );

Var_dump (isset ($ a); // FALSE
Var_dump (isset ($ a, $ B); // FALSE

$ Foo = NULL;
Var_dump (isset ($ foo); // FALSE

?>

This is also effective for elements in the array:

The code is as follows:

$ A = array ('test' => 1, 'Hello' => NULL );

Var_dump (isset ($ a ['test'); // TRUE
Var_dump (isset ($ a ['Foo'); // FALSE
Var_dump (isset ($ a ['Hello'); // FALSE

// 'Hello' is equal to NULL, so it is considered to be unassigned.
// If you want to check the NULL key value, try the following method.
Var_dump (array_key_exists ('hello', $ a); // TRUE

?>

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.