Home  >  Article  >  Backend Development  >  Check if a person is eligible to vote via PHP

Check if a person is eligible to vote via PHP

藏色散人
藏色散人Original
2021-08-11 10:36:584259browse

In daily life, everyone should have seen various small voting functions, and not everyone can vote. Voting requirements will be designed according to specific circumstances, and only users who meet the requirements are eligible to vote. So today I will teach you how to use PHP to determine whether a person is qualified to vote.

This article will introduce the most basic PHP code implementation method, which is relatively friendly to newcomers. Once you master these basic knowledge, you can draw inferences from one example to other cases.

Here I have a small request: the minimum age required to vote is 18 years old. If you are younger than 18 years old, you are not allowed to vote.

So with this small requirement, we give the following method:

The PHP code is as follows:

<?php

function check_vote() //声明方法
{
    $name = "小王";
    $age = 19;
    if ($age >= 18) {
        echo $name . ", 你有资格投票";
    } else {
        echo $name . ", 你没有资格投票. ";
    }
}
check_vote(); //调用check_vote()

Output result:

小王, 你有资格投票

In In the above code, we declare a check_voteh function, and then give an example user "Xiao Wang is 19 years old". Finally, we use the most basic if...else statement to determine whether the user is older or younger than 18 years old. If greater than If it is equal to 18 years old, it will output "You are eligible to vote". If it is less than 18 years old, it will output "You are not eligible to vote".

In fact, this article is equivalent to learning the if...else conditional statement in PHP through a simple example. The official description of this statement is when the condition is true (true) When the condition is false, the code is executed; if the condition is false, the code on the other end is executed.

Then there are other conditional statements in PHP:

if语句:如果指定条件为真,则执行代码
if...elseif....else语句:根据两个以上的条件执行不同的代码块
switch语句:选择多个代码块之一来执行

Finally, I would like to recommend a few articles about the specific implementation methods of the voting function:

1.《ThinkPHP5 jQuery MySql method to implement voting function

2.《Use redis to make counters to improve the voting system

Related recommendations The latest and most comprehensive《 PHP Video Tutorial》~Come and learn!

The above is the detailed content of Check if a person is eligible to vote via PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn