PHP, is there a simple way to determine whether a submitted field is not empty?

Source: Internet
Author: User
PHP, is there a simple way to determine whether a submitted field is not empty? PHP, is there a simple way to determine whether a submitted field is not empty?

Reply content:

PHP, is there a simple way to determine whether a submitted field is not empty?

A submitted field! Yes_POST,_GETEven_REQUESTInstead of simply discussing$varAh!

Three protection cases:

  1. This field does not exist in the submission. (If the subscript does not exist, a runtime error is directly thrown)
  2. The submitted field is empty.
  3. Submitted Fieldstrim()Enter

For native PHP, the recommended steps are as follows:

  1. The principle is preprocessing first.$_POST.
  2. Set$_POSTRemoves spaces at the beginning and end of all elements.
  3. Set$_POSTAll the empty elements are thrown.
  4. In this way, the surviving elements in the array are not empty. Determine whether an element exists.

Pay attention to steps 2 and 3. Never be dumb.foreachLoop by yourself, preferably using several traversal functions in the array function. Code:

$_POST = array_map('trim', $_POST);$_POST = array_filter($_POST);$result = array_key_exists('fieldname', $_POST);if ($result) {    // do whatever you wish}

This is easy enough, but the problem is poor portability (because it depends on the preprocessing done by the front side ). If you want to be portable, simple, and comprehensive, write your own functions.

if(trim($var) != ''){    // code here}

if(!empty($var)){    //code here}

I personally prefer to use isset () for judgment.

if( isset( $_GET['password'] ) ) {    // do something}

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.