How to use ISSET (), empty (), and is_numeric () functions for PHP form verification

Source: Internet
Author: User
Tags php form
Share the usage of the three functions ISSET (), empty (), and is_numeric () in PHP form verification. For more information about php learning, see. ISSET (); -- this parameter is suitable for detecting whether this parameter exists.
Definition and scope of action: used to test whether a variable has a value (including 0, FALSE, or an empty string, but cannot be NULL), that is, "http: // localhost /? Fo = "can also pass detection, so it is not applicable. However, if the "http: // localhost/" parameter does not contain the fo parameter, you can use isset for detection. in this case, isset ($ _ GET ['fo']) returns false.
Not applicable: This function is not suitable for verifying text in html forms. To check whether the user input text is valid, you can use empty ();
Empty (); -- the best function.
Definition and scope of action: used to check whether a variable has a null value, including: null string, 0, null, or false, that is, "http: // localhost /? Fo = "or" http: // localhost /? When fo = 0 ", empty detects true and does not apply to values: 0.
Is_numeric (); -- this parameter is only applicable to the detection of numbers. if the parameter name does not exist, an error occurs. Therefore, it is not suitable for the first-level detection.
Comprehensive example:
The code is as follows:
Ini_set ("display_errors", 1 );
// Ini_set ("error_reporting", E_ALL); print_r
Error_reporting (E_ALL );
$ A = NULL;
If (isset ($ a) echo 'isset of variable $ A' is true ';
Echo 'isset :';
If (isset ($ _ GET ['fo']) {
Echo 'isset of variable/'Fo/'is true and the variable is usable ';
} Else {
Echo 'isset of variable/'Fo/'is false and no variable settings ';
}
Echo 'empty :';
If (empty ($ _ GET ['fo']) {
Echo 'empty of variable/'Fo/'is true, that is, null or invalid value ';
} Else {
The empty of echo 'variable/'Fo/'is false and has a value ';
}
Echo 'is _ numeric :';
If (is_numeric ($ _ GET ['fo']) {// if the parameter does not contain the fo parameter, an error occurs.
Echo 'is_numeric of the variable/'Fo/'is true, a number ';
} Else {
The is_numeric of echo 'variable/'Fo/'is false, not a number ';
}
Echo "/$ _ GET ['fo'] = :";
If ($ _ GET ['fo'] = '') {// if the parameter does not contain the fo parameter, an error occurs.
Echo 'Fo no value, empty string ';
} Elseif ($ _ GET ['fo']! = ''){
Echo 'Fo has a value, not /'/'.';
}
Echo "/$ _ GET ['sex'] ='m :";
If ($ _ GET ['Sex '] = 'm') {// an error occurs when no sex variable exists in the parameter.
Echo 'mal ';
} Elseif ($ _ GET ['sex'] = 'F '){
Echo 'femal ';
}
?>




Untitled Document




Pass a valid value to pass a null value to 0



Gender: Male Gender: Female



Clear





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.