PHP

PHP Program to Check Whether a Person is Eligible to Vote

In this tutorial, we’ll write a simple PHP program to check if a person is eligible to vote based on their age. The eligibility criterion is straightforward: a person must be 18 years or older to be eligible for voting.

We’ll create a web form that asks the user to enter their age, and based on the input, the program will determine whether the person is eligible to vote or not.




Program Logic:

  1. The program prompts the user to enter their age using an HTML form.
  2. The form sends the age value to the PHP script using the GET method.
  3. The PHP script checks if the entered age is greater than or equal to 18.
  4. If the age is 18 or more, the program will print “Eligible for Voting!”.
  5. If the age is less than 18, it will print “Not Eligible for Voting!”.

PHP Code:

Explanation of the Code:

  1. HTML Form:
    • The form contains a text input field for the user to enter their age (<input name="age" placeholder="Enter your age" type="number">).
    • The form uses the GET method to submit the entered data, sending it to the same PHP page.
    • The submit button (<input name="btn" type="submit" value="Submit Age">) triggers the submission of the form.
  2. PHP Script:
    • Input Retrieval: The PHP script checks if the age value is submitted using the isset() function (if(isset($_GET["age"]))).
    • Age Validation: The script retrieves the entered age from the $_GET array and compares it to 18. If the age is 18 or more, it prints “Eligible for Voting!”, otherwise it prints “Not Eligible for Voting!”.
  3. Output:
    • After the form submission, the webpage will display the entered age and show whether the person is eligible to vote based on the input.

Example Output:

If the user enters 20:

If the user enters 16:

Conclusion:

This simple PHP program demonstrates how to check whether a person is eligible to vote based on their age. By using basic form handling and conditional statements, we can easily create interactive web applications that perform real-time validation.

Write a program to check whether a person is eligible to vote or not in PHP

Output:

Leave a Comment