PHP

PHP Program to Check Voting Eligibility Based on Age

In this tutorial, you will learn how to write a simple PHP program to check if a person is eligible to vote based on their age. The eligibility rule is straightforward: a person must be 18 years or older to vote.

We will create a web form where the user can enter their age. Based on the input, the PHP script will determine and display whether the user is eligible to vote.

Program Logic

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

PHP Code Example


Code Explanation

HTML Form:

  • The form contains an input field for the user to enter their age: htmlKodu <input name="age" placeholder="Enter your age" type="number" required>
  • The form uses the GET method to send the data.
  • The submit button labeled “Check Eligibility” triggers the form submission.

PHP Script:

  • Input Retrieval: The script checks if the age parameter is set using isset($_GET["age"]).
  • Type Casting: It safely converts the input into an integer with (int) $_GET["age"] to avoid unexpected results.
  • Eligibility Check: The script compares the age to 18:
    • If the age is 18 or greater, it prints “Eligible for Voting!”.
    • Otherwise, it prints “Not Eligible for Voting!”.

Example Output

Case 1: User Enters 20

Case 2: User Enters 16


Conclusion




This simple PHP project shows how to check voting eligibility based on age using a web form and basic PHP scripting.
By utilizing HTML form inputs and conditional statements in PHP, you can build interactive web applications that validate user data in real time.

Feel free to experiment with this code and customize it for different eligibility criteria!

Leave a Comment