In this tutorial we are writing a PHP Program to Check Eligibility for voting.
To check that a person is eligible for voting or not, we need to check whether person’s age is greater than or equal to 18. For this we are reading age in a variable a and checking the condition a>=18, if the condition is true, “person will be eligible for voting” else not.
Program Source Code : PHP program to check eligibility for voting
PHP Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <html> <head> <title>PHP Test</title> </head> <body> <form method="get"> <input name="age" placeholder="enter age"><br> <input name="btn" type="submit" value="Submit Age"> <?php if(isset($_GET["age"])) { //solution $age = $_GET["age"]; echo "<h2> Your Age:$age<h2>"; if ($age >= 18) echo "<h1>Eligible for Voting!</h1>"; else echo "<h1>Not Eligible for Voting!</h1>"; } ?> </form> </body> </html> |
This is a PHP program that creates a simple web page with a form for the user to enter their age and check if they are eligible to vote based on the age they enter. Here’s what the code does:
- The program starts with an HTML template which creates a basic web page with a form and a submit button
- The form has a text input field with a
name
attribute set to “age” and a placeholder text “enter age”. - The form also has a submit button with
type="submit"
and aname
attribute set to “btn”, which when clicked will submit the form data. - The PHP code is enclosed between
<?php
and?>
tags. It first checks if the form has been submitted by checking the presence of the “age” variable in the$_GET
array. - If the form has been submitted, it gets the value of the “age” input field from the
$_GET
array and assigns it to the variable$age
. - Then it uses an
if-else
statement to check if the age is greater than or equal to 18. If the age is greater than or equal to 18, it prints a message indicating that the user is eligible for voting. Else, it prints that the user is not eligible for voting. - The final output will be a webpage where the user can input their age, after submitting it will show if the user is eligible for voting or not.
Output: