How to use PHP form validation functions isset (), Empty (), Is_numeric ()

Source: Internet
Author: User
Tags php form php form validation
ISSET ();--suitable for detecting the presence of this parameter.
Definition and scope: used to test whether a variable has a value (including 0,false, or an empty string, but not null), that is: "http://localhost/?fo=" is also detectable and therefore not applicable. However, if the "http://localhost/" parameter does not contain the FO parameter, it can be detected with isset, at which point the isset ($_get[' fo ') returns false.
Not applicable: This function is not suitable for validating the text in an HTML form in an efficient way. To check whether the user input text is valid, you can use empty ();

Empty ();--The best use of a function.
Definition and scope: used to check if a variable has a null value: include: empty string, 0,null or FALSE, that is: "http://localhost/?fo=" or "http://localhost/?fo=0", the result of empty detection is is ture, not applicable: does not apply to the detection of a parameter that can be 0.


Is_numeric ();--only for the detection of numbers, but if the parameter name does not exist, it will be wrong, so it is not suitable for the first layer detection.
Comprehensive Example:

<?php
Ini_set ("Display_errors", 1);
Ini_set ("error_reporting", E_all); Print_r
Error_reporting (E_all);
$a =null;
if (Isset ($a)) echo ' variable $ A ' isset is true ';
Echo ' if (Isset ($_get[' fo ')) {
echo ' variable/' fo/' isset is true, variable available ';
}else{
echo ' variable/' fo/' Isset is false, no variable set ';
}
Echo ' if (Empty ($_get[' fo '))) {
echo ' variable/' fo/' empty is true, that is, null or invalid value ';
}else{
echo ' variable/' fo/' of empty is false, has value ';
}
Echo ' if (Is_numeric ($_get[' fo '))) {//There is no fo parameter in the parameter, there is an error.
echo ' variable/' fo/' is_numeric is true, is the number ';
}else{
echo ' variable/' fo/' Is_numeric is false, not a number ';
}
echo "echo ' fo no value, empty string ';
}elseif ($_get[' fo ']!= ') {
Echo ' fo has value, not for/'. ';
}
echo "if ($_get[' sex ']== ' m ') {//error occurs when there is no sex variable in the parameter.
Echo ' The man ';
}elseif ($_get[' sex ']== ' f ') {
echo ' female ';
}
?>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title> Untitled Document </title>
<body>
</body>
<p>
<a href= "? fo=jack" > RMS </a> <a href= "? fo=" > Null value </a> <a href= "? fo=0" > Pass 0 value </a>
<br/><br/>
<a href= "? sex=m" > Gender: Male </a> <a href= "? sex=f" > Gender: Women </a>
<br/><br/>
<a href= "/" > Empty </a>
<br/><br/>
<input type= "text" value= "<?php echo $_get[' fo ']!= '? $_get[' fo ']: ';? > "size=" 155 "/>
</p>

  • 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.