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

Use PHP to determine leap year examples

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

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

        

查询闰年

//未提交时抑制‘非法访问’提示
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:

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