<meta charset="utf8">
<p>平年、闰年计算器</p>
<form>
<input type="text" name="num1">
<input type="submit" value="判断">
</form>
<?php
$num1=$_GET['num1'];
if(!is_numeric($num1))
{echo '请正确输入年份';
exit;}
if($num1%4==0 and $num1%100!==0 or $num1%400==0)
{echo $num1.'年是闰年';}
else
{echo $num1.'年是平年';}
?>
海风2019-10-09 11:09:14
第一次进入时$_GET['num1']不存在,给一个量变赋不存在的值是会出错的,所以给$num1一个初始值,如:$num1 = isset($_GET['num1']) ? $_GET['num1'] : 2019;
如果不想一刷新就出来2019年是平年这句话,程序修改如下:
<?php
if (!empty($_GET['num1'])){
$num1=$_GET['num1'];
if(!is_numeric($num1))
{echo '请正确输入年份';
exit;}
if($num1%4==0 and $num1%100!==0 or $num1%400==0)
{echo $num1.'年是闰年';}
else
{echo $num1.'年是平年';}
}
?>