简体   繁体   中英

How can I echo the third condition in this php IF STATEMENT?

I have developed school mark-sheet application using phpmysql. In the mark-sheet, if a student fails in one of the subjects, he/she will be declared 'Failed' in the result section of the mark-sheet. I could do that job perfectly with this code.

Today the principal of the school has come to me and said that this is not in use today, and told me to change my code to something like: if a student fails in two subjects, he should be declared 'Failed', but if he fails only in one subject, echo 'Simple`, and if he passes in all the subjects, he should be declared 'Passed'. I have been spending long hours trying to figure out how I would modify my code but still I could not come up with solution. So, I come here asking for help. Any suggestion is warmly welcome. Thanks a lot in advance. Here is my code:

<?php 

$all = array(41, 55, 56, 39, 29, 47);  //Mark in each subject. Pass mark is 30

// second, just iterate over them till you find one value -30
for($i=0; $i < count($all); $i++){
     if($all[$i] < 30) $sum = 1;
}

echo (!empty($sum)) ? 'Failed' : 'Passed';//'Simple' must be included here, but I still could not find the solution.

?>

First of all create a initial variable with 0, and increment it inside for loop.

$all = array(41, 55, 56, 39, 29, 47);  //Mark in each subject. Pass mark is 30
$sum = 0;
// second, just iterate over them till you find one value -30
for($i=0; $i < count($all); $i++){
    if($all[$i] < 30) $sum++;
}

Now check here with multiple conditions.

if($sum == 0) {
    echo 'passed';
} elseif($sum == 1) {
    echo 'simple';
} else {
    echo 'failed';
}

Just create a counter to know the number of failatures and then use conditions for the different prints.

<?php 

$all = array(41, 55, 56, 39, 29, 47);  //Mark in each subject. Pass mark is 30

$numFails = 0;

// second, just iterate over them till you find one value -30
for($i=0; $i < count($all); $i++){
     if($all[$i] < 30){
         $numFails++;  //incrementing the number of failatures
     }
}

if($numFails > 1){
    echo "Failed";
}
else if($numFails == 1){
   echo "Simple";
}
else{
   echo "Passed";
}

?>

Try to write good code. As clear and easy to read as possible.

Also Name the variables with a meaning.

$count = 0;
//define the length outside of the loop - 
//otherwise PHP will call the function count() each iteration 
//which is bad practice performance wise
$length = count($all);

for($i=0; $i < $length; $i++){
     if($all[$i] < 30) ++$count;
     if($count >= 2) {
         $sum = 1; 
     }
}

This increments $count if there's a fail, and when there's 2 fails then it sets $sum, which you can then check for as you have done already.

您可以遍历所有标记,并为每个小于30的标记将$ sum加1。然后检查$ sum是否为<2 OR> 2,应该是。

Why don't you sort the array, skip iteration and look if the first two values are below 30, or the first value is below 30 and the second above 30 and the last one, if the first value is already above 30?

Like this:

$all = array(41, 55, 56, 39, 29, 47);

sort($all); 

if ($all[0] < 30 && $all[1] < 30) echo "Failed";
elseif ($all[0] < 30 && $all[1] >= 30) echo "Simple";
elseif ( $all[0] >=30 ) echo "Passed";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM