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
,_GET
Even_REQUEST
Instead of simply discussing$var
Ah!
Three protection cases:
- This field does not exist in the submission. (If the subscript does not exist, a runtime error is directly thrown)
- The submitted field is empty.
- Submitted Fields
trim()
Enter
For native PHP, the recommended steps are as follows:
- The principle is preprocessing first.
$_POST
.
- Set
$_POST
Removes spaces at the beginning and end of all elements.
- Set
$_POST
All the empty elements are thrown.
- 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.foreach
Loop 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}