In a previous article "How to use PHP switch to display the day of the week", I introduced how to use the PHP switch statement to display the day of the week, in which Switch is one of the PHP conditional statements. Today we are going to introduce the use of another conditional statement~
The main content of this article is just as the title says. The specific problem description is "How to write a PHP program that uses if-else statements to check student performance based on scores"?
For example, if we divide students' grades into four grades: A, B, C, and unqualified, then we only need to judge the grade range based on the scores given!
Without further ado, let’s go directly to the code:
The PHP code is as follows:
<?php $marks = 40; if ($marks>=90) { $grade = "甲等"; } else if($marks>=75) { $grade = "乙等"; } else if($marks>=60) { $grade = "丙等"; } else { $grade = "不合格"; } echo "学生成绩: $grade";
Here we randomly gave a score of 40 , the judgment result is as follows:
学生成绩: 不合格
In the above code:
If the score is 90 or higher, the grade will be A; if the score is between 75 and 89, the grade will be B grade;
If the score is between 60 and 74, the grade is C grade; if the score is below 60, the student will fail.
Note:
if...else
Statement: execute a block of code when the condition is true, and execute another block of code when the condition is not true;
The syntax is:
if (条件) { 条件成立时执行的代码; } else { 条件不成立时执行的代码; }
Here is a brief introduction to everyone:
if...elseif....else
Statement: in several To execute a block of code when one of the conditions is true, use the if....elseif...else statement.
its grammar
if (条件) { if 条件成立时执行的代码; } elseif (条件) { elseif 条件成立时执行的代码; } else { 条件不成立时执行的代码; }
Finally, I would like to recommend the latest and most comprehensive "PHP Video Tutorial"~ Come and learn!
The above is the detailed content of How to check student grade level using PHP if-else statement. For more information, please follow other related articles on the PHP Chinese website!