Original: The use of 3 functions isset (), Empty (), and Is_numeric () for forms validation
This article briefly about the form validation in PHP three functions, it should be more common, and finally give some examples, see below.
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", Empty detection results are ture, not applicable range: not used to detect the parameters 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>
How to use the 3 functions of form Validation isset (), Empty (), Is_numeric ()