PHP

Write a program to check whether a person is eligible to vote or not in PHP2 min read

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:

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:

  1. The program starts with an HTML template which creates a basic web page with a form and a submit button
  2. The form has a text input field with a name attribute set to “age” and a placeholder text “enter age”.
  3. The form also has a submit button with type="submit" and a name attribute set to “btn”, which when clicked will submit the form data.
  4. 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.
  5. 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.
  6. 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.
  7. 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:

Leave a Comment