Thursday, 5 October 2017

PHP script to find leap year from a any year given


To find a leap year or not with php script, you can use vscode or some other app that can write php script.
·        Step1 : open your vscode
·        Step 2: open a file and name it leap_year.php
·        Step 3: inside your new file open your html tags and give it a title.
·        Step 4: create a simple form that will post and submit inside your html tags.
·        Step 5: open php tags and write your code.

Below is sample of php script to write a leap year.


<html>
<head>
<title></title>
</head>
<body>
<h2>PHP Script to find Leap year or not</h2>

// A form that create a post and submit
<form action="" method="post">
                        <input type="text" name="year" />
                        <input type="submit" />
            </form>

</body>


</html>

<?php

            if( $_POST )
            {          
                       
                        $year = $_POST[ 'year' ];
                       
                       
                        if(!is_numeric($year))
                        {
                                    echo "Strings not allowed, Input should be a number";
                                    return;
                        }
                       
                       
                        if( (0 == $year % 4) and (0 != $year % 100) or (0 == $year % 400) )
                        {
                                    echo "$year is a leap year"; 
                        }
                        else
                        {
                                    echo "$year is not a leap year"; 
                        }

            }
           


?>

No comments:

Post a Comment