Home >Backend Development >PHP Tutorial >Use PHP to determine leap year examples

Use PHP to determine leap year examples

*文
*文Original
2017-12-22 11:05:334346browse

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 &#39;请输入年份&#39;;return;}
 
//不是点击提交按钮过来的,禁止访问
if(isset($_POST[&#39;chaxun&#39;])){
        $year = $_POST[&#39;year&#39;];
}else{
        echo &#39;非法访问&#39;;
        return;
}
//此时post过来的数据类型是字符串类型&#39;2000&#39;,判断是否是数值型,并通过$year+0转换为数字类型
if(is_numeric($year)){$year = $year + 0;}else{die(&#39;非法输入&#39;);}
 
//注:测试数据类型 echo gettype($year);//此时$year是数字类型,判断是否是整数
 
if(is_int($year)){
        //主程序
        if($year<1000 or $year>9999){die(&#39;超出查询范围&#39;);
        }elseif($year%4 == 0 && $year%100!=0 || $year%400==0){
                echo $year.&#39;是闰年&#39;;
        }else{echo $year.&#39;是平年&#39;;}
 
}else{echo &#39;请输入四位整数数字&#39;;}
//或方法二:strpos($year,&#39;.&#39;)是不允许有小数点,间接判断是否是整数
/*if(is_numeric($year)&&!strpos($year,&#39;.&#39;)){
 
        if($year%4 == 0 && $year%100!=0 || $year%400==0){
        echo $year.&#39;是闰年&#39;;
        }else{
                echo $year.&#39;是平年&#39;;
        }
 
}else{
        echo &#39;请输入整数数字&#39;;
}*/

Related reading:

First introduction to PHP - Basic Grammar php7 Grammar PHP Development Practical Examples 1200 Sublime PHP Grammar Check

##PHP Grammar (Syntax)

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!

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