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