根据条件php更改变量值

I have a class name that should change based on certain conditions but either my syntax or my logic seems to be incorrect as it doesn't work:

if ( $hometeam && ($homescore > $awayscore) || $awayteam && ($awayscore > $homescore)){
                        $status= 'win'; 
                    }                       
if ( $hometeam && ($homescore < $awayscore) || $awayteam && ($awayscore < $homescore)) {
                        $status= 'lose';
                    }                       
if ($homescore == $awayscore) {
                        $status= 'draw';
                    }

My elements each have a class="<?php echo $status; ?>" and I want them to be styled differently depending on the value of $status. I didn't use else if because I didn't want it to apply the value and stop but it looks like that is what is happening.

Edit: Some clarification:

<ul class="match group">    
                <li>
                    <ul class="team1 <?php echo $status; ?>">
                        <li class="teamname"><h2><?php echo $homename; ?></h2></li>  
                        <li class="teamscore"><?php echo $homescore; ?></li>
                    </ul>
                </li>
                <li>
                    <ul class="team2 <?php echo $status; ?>">
                        <li class="teamname"><h2><?php echo $awayname?></h2></li>    
                        <li class="teamscore"><?php echo $awayscore?></li>                              
                    </ul>
                </li>
                <li><ul class="matchinfo">
                        <li><a href="reports.html#report4"><button>Get Report</button></a></li>
                    </ul>
                </li>
</ul>

You need to group your conditions better.

if ( ($hometeam && ($homescore > $awayscore)) || ($awayteam && ($awayscore > $homescore))) 
{
  $status= 'win'; 
}                       
else if ( ($hometeam && ($homescore < $awayscore)) || ($awayteam && ($awayscore < $homescore))) 
{
  $status= 'lose';
} 
else 
{
  $status= 'draw';
}

By your logic, there are only two possible outcomes for the value of $status: 'lose' or 'draw'.

Why is this? Basic logic says that unless there is a tie, a game will always have a winner. Your if statement lines up with this. Either $homescore > $awayscore or $homescore < $awayscore (or there is a tie). So in the event of a non-tie, one of the sides of your first if will evaluate to true, and therefore the entire condition will be true since || needs only one truthy. So unless there is a tie, $status will be set to 'win'.

Moving on, basic logic will also tell us that a game always has a loser unless there is a tie. Again, your code conforms to this. In the event of a non-tie, one of the score will be less than the other and therefore your second if will evaluate to true like the first did. Now $status = 'lose'.

Now if there is a tie, $status would equal 'draw', but otherwise $status will always be 'lose' because if the game has a winner, it also has a loser and $status is set to 'lose' after it is set to 'win'.


What you need here is a variable for the winner, loser, and for a draw. Like so:

$winner = '';
$loser  = '';
$draw   = false;

if($homescore > $awayscore) {
    $winner = 'home';
    $loser  = 'away';
} else if($homescore < $awayscore) {
    $winner = 'away';
    $loser  = 'home';
} else if($homescore === $awayscore) {
    $draw   = true;
}

(Codepad Demo)

This code also makes use of else if, because you don't need to evaluate the other conditions if you've determined who won and lost.

if ($homescore > $awayscore) {
        $homestatus = 'win';
        $awaystatus = 'lose';
    } else if ($homescore < $awayscore) { 
        $homestatus = 'lose';
        $awaystatus = 'win';
    } else {
        $homestatus = 'draw';
        $awaystatus = 'draw';
    }

<ul class="match group">    
            <li>
                <ul class="team1 <?php echo $homestatus; ?>">
                    <li class="teamname"><h2><?php echo $homename; ?></h2></li>  
                    <li class="teamscore"><?php echo $homescore; ?></li>
                </ul>
            </li>
            <li>
                <ul class="team2 <?php echo $awaystatus; ?>">
                    <li class="teamname"><h2><?php echo $awayname?></h2></li>    
                    <li class="teamscore"><?php echo $awayscore?></li>                              
                </ul>
            </li>
            <li><ul class="matchinfo">
                    <li><a href="the_permalink()"><button>Get Report</button></a></li>
                </ul>
            </li>
</ul>