This article is a learning guide for novices. Judgment is the basic grammar in programming and the most important. This article uses examples of judgment on submitted data to familiarize novices with the use of judgment in PHP.
//Two basic knowledge points:
//1. Security factors: How to prohibit non-submission access?
== Determine whether it is a page submission by whether there is a name value of the submit button in the POST data
//2, data type conversion: the year number submitted by the form form, it is found that the PHP received it is a string type, how to Convert?
== Convert by automatically converting '2000'+0 to int type during operation
<h3 align="center">查询闰年</h3> <form action="" method="post"> <input type="text" name="year" value=""> <input type="submit" value="查询" name="chaxun"> </form>
//未提交时抑制‘非法访问’提示 if(empty($_POST)){echo '请输入年份';return;} //不是点击提交按钮过来的,禁止访问 if(isset($_POST['chaxun'])){ $year = $_POST['year']; }else{ echo '非法访问'; return; } //此时post过来的数据类型是字符串类型'2000',判断是否是数值型,并通过$year+0转换为数字类型 if(is_numeric($year)){$year = $year + 0;}else{die('非法输入');} //注:测试数据类型 echo gettype($year);//此时$year是数字类型,判断是否是整数 if(is_int($year)){ //主程序 if($year<1000 or $year>9999){die('超出查询范围'); }elseif($year%4 == 0 && $year%100!=0 || $year%400==0){ echo $year.'是闰年'; }else{echo $year.'是平年';} }else{echo '请输入四位整数数字';}
//或方法二:strpos($year,'.')是不允许有小数点,间接判断是否是整数 /*if(is_numeric($year)&&!strpos($year,'.')){ if($year%4 == 0 && $year%100!=0 || $year%400==0){ echo $year.'是闰年'; }else{ echo $year.'是平年'; } }else{ echo '请输入整数数字'; }*/
Related reading:
PHP Basic Tutorial 2 php7 php environment building php from entry to proficiency
The above is the detailed content of Use PHP to determine leap year examples. For more information, please follow other related articles on the PHP Chinese website!