How does php determine whether the get or post value exists? this problem has plagued me for a long time: isset, empty, and is_array, please give me some advice on how to determine whether the get or post value exists in php. This problem has plagued me for a long time: isset, empty, or is_array. please give me some advice.
Reply content:
How does php determine whether the get or post value exists? this problem has plagued me for a long time: isset, empty, or is_array. please give me some advice.
Isset () is used to determine whether the variable is defined. empty () is used when the variable has been defined (if the variable is not defined, the error variable is undefinded ), determines whether the variable is null, an empty string, and an empty array is considered null by empty (). true is returned. For example, if you upload a form $ user ('name' => 'Tom ', 'nickname' => ''), you can use empty () to determine whether $ use ['nickname'] has been filled in by users. for example, your form contains a password field, however, third-party login and registration users do not have a password field. Therefore, you need to determine isset ($ user ['password']). The difference between isset and empty is essentially the same, I hope the example can help you with your understanding ~
You can use $ _ SERVER ['request _ method'] to determine the REQUEST type.
Please refer to the official explanation.
$ _ SERVER-SERVER and execution environment information
Come and teach
$ Apply_name = isset ($ _ POST ['apply _ name'])? $ _ POST ['apply _ name']: '';
Or
$ Apply_name = isset ($ _ GET ['apply _ name'])? $ _ GET ['apply _ name']: '';
If you don't even know get and post
$ Apply_name = isset ($ _ REQUEST ['apply _ name'])? $ _ REQUEST ['apply _ name']: '';
My personal statement is [check all], which can avoid various errors. After all, security and comprehensiveness are usually used in common projects.
// All my projects have a public file including custom_function.php. Function check_var ($ var, $ default = '') {return (isset ($ var) and! Empty ($ var ))? $ Var :(! Empty ($ default )? $ Default: false);} // so that I can call the global function detection variable in any of my files: $ array = ['id' => 123]; var_dump (check_var ($ array ['id']); // output int (123) var_dump (check_var ($ array ['code']); // output bool (false) var_dump (check_var ($ array ['code'], 'not set'); // output string (7) "not set"
First, determine the functions of the isset, empty, and is_null variables, and then use the corresponding functions according to your use scenario:
Php empty, isset, is_null comparison reference 1
Php empty, isset, is_null comparison reference 2
Php empty, isset, and is_null comparison reference 3
Variable judgment Appendix