Home >Backend Development >PHP Tutorial >How to use the three functions ISSET(), empty(), and is_numeric() in PHP form validation_PHP Tutorial

How to use the three functions ISSET(), empty(), and is_numeric() in PHP form validation_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:25:141151browse

ISSET();——Suitable for detecting whether this parameter exists.
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=" can also be passed detection and therefore not applicable. But if the "http://localhost/" parameter does not contain the fo parameter, you can use isset to detect it. In this case, isset($_GET['fo']) returns false.
Not applicable: This function is not suitable for validating text in html forms in an efficient way. To check whether the user input text is valid, you can use empty();
empty(); - the best function to use.
Definition and scope: used to check whether the variable has a null value: including: empty string, 0, null or false, that is: "http://localhost/?fo=" or "http://localhost/ ?fo=0", the results detected by empty are all true, not applicable scope: not suitable for detecting parameters that can be 0.
is_numeric(); - only suitable for detecting numbers, but if the parameter name does not exist, an error will occur, so it is not suitable for the first level of detection.
Comprehensive example:

Copy code 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 'Variable$ The isset of a is true';
echo '

isset situation:

';
if(isset($_GET['fo'])){
echo 'variable The isset of /'fo/' is true and the variable is available';
}else{
echo 'The isset of the variable /'fo/' is false and no variable is set';
}
echo '

empty situation:

';
if(empty($_GET['fo'])){
echo 'empty of variable /'fo/' is true, that is, empty Value or invalid value';
}else{
echo 'The empty of variable /'fo/' is false and has a value';
}
echo '

is_numeric case:< ;/h2>';
if(is_numeric($_GET['fo'])){ //When there is no fo parameter in the parameter, an error occurs.
echo 'The is_numeric of variable /'fo/' is true, it is a number';
}else{
echo 'The is_numeric of variable /'fo/' is false, it is not a number';
}
echo "

/$_GET['fo']='' case:

";
if($_GET['fo']==''){ // If there is no fo parameter in the parameter, an error occurs.
echo 'fo has no value, an empty string';
}elseif($_GET['fo']!=''){
echo 'fo has a value, not /'/'. ';
}
echo "

/$_GET['sex']='m' case:

";
if($_GET['sex']= ='m'){ //An error occurs when there is no sex variable in the parameter.
echo 'male';
}elseif($_GET['sex']=='f'){
echo 'female';
}
?>




Untitled Document



< ;p>
Pass a valid value Pass an empty value Pass 0 value



Gender : Male Gender: Female



Clear






www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324138.htmlTechArticleISSET();——Suitable for detecting whether this parameter exists. Definition and scope: used to test whether a variable has a value (including 0, FALSE, or an empty string, but not NULL)...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn