How does php determine whether the get or post value exists?

Source: Internet
Author: User
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

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.