简体   繁体   中英

If statement with and won't work?

Very stupid question, i am C# kid, i also programming 4 years in php, but sometimes if statement won't work when i use 'and' or '&&' inside.

For example current problem is this:

    while($row = $res->fetch()){
        if(($row['Season'] != "2015/2016") && ($row['Source'] != "xmlsoccer")){ 
        $i3++; if($i3==1)hisHeader();
        $eventy = $row['HomeTeam'] . '-v-' . $row['AwayTeam'];
        $stagey = $row['League'];

        $datetime = new DateTime($row['Date']);
        $la_time = new DateTimeZone($zone);
        $datetime->setTimezone($la_time);
        $date = $datetime->format('Y-m-d');
        $time = $datetime->format('H:i');

        $url = toAscii($eventy) . "/" . toAscii($stagey) . "/"  . code36($row['HisID']);
        echo "<tr class='brown' style='height:20px;'>" , "<td style='width:120px; border:1px solid #444;'>" , "<matches-list>" , $date , "</matches-list>" , "</td>" , "<td style='width:60px; border:1px solid #444;'>" , "<matches-list>" , $time , "</matches-list>" , "</td>" , "<td class='nav' style='border:1px solid #444;'>" , "<matches-list>" , "<a href='".$url."' title='$matchtitle'>" ,$row['EventName'] ," (<span style='color:#555'>{$row['League']}</span>)", "</matches-list>","</td>";
        if($row['Status'] != 0){    
          echo "<td style='width:60px; border:1px solid #444;'>", "<matches-list>" , 
          $row['HalfTimeHomeGoals'] . ' : ' . $row['HalfTimeAwayGoals'], "</matches-list>", "</td>", 
          "<td style='width:60px; border:1px solid #444; color: #007AFF'>","<matches-list>" , 
          $row['HomeGoals'] . ' : ' . $row['AwayGoals'], "</matches-list>","</td></tr>";
      }
      else{
        echo "<td style='width:60px; border:1px solid #444; color: #777777' colspan='2' style='text-align:center !important;'>",
        "<matches-list>", "NO RESULTS YET" ,"</matches-list>","</td>";
      }
    }
    }
    echo "</tbody></table>";
}

Why

if(($row['Season'] != "2015/2016") && ($row['Source'] != "xmlsoccer")){ 

won't work? I have millions rows in database and i don't want xmlsoccer data for 2015/2016, but when i run this it returns nothing.

When i remove just this && ($row['Source'] != "xmlsoccer") everything works fine.

I am 100% sure that is everything correct, for example when i dump

var_dump($row['Season'] . " " . $row['Source']); 

it returns for example, '2013/2014' 'xmlsoccer', but nothing inside if statement...

Keep in mind the Chasles relation. So to explain, let's do some mathematics :

if(a && b) can be associated in :

if a and b is true so result is true

But if you need to invert this you will think of :

if(not a and not b) which will be represented like following :

if ( a and b is not true so c is true)

But we can translate this last sentence with the Chasles theorm that says :

A + B = C

(A + B)\\ = C

so with Chasles relation the result is

A\\ . B\\ = C A\\ . B\\ = C (the dot means OR )

Which corresponds to A || B = C

So we can say that (A + B)\\ = C is equivalent to A\\ + B\\ = C which means in programming :

! (a && b) ! (a && b) is true is equivalent to !a || !b !a || !b is also true

Finally you can write those 2 conditions that are the sames :

  1. if( ! ( $row['Season'] == "2015/2016") && $row['Source'] == "xmlsoccer" ) )
  2. if( $row['Season'] != "2015/2016") || $row['Source'] != "xmlsoccer" )

Firstly filter on the SQL layer, it should then return only what you need. Secondly you clearly ask for everything that isn't xmlsoccer

($row['Source'] != "xmlsoccer")

But you are confused when you dump the values that you see "2013/2014" and "xmlsoccer" and dont go into the if statement. From what I understand from your question you want everything that is "xmlsoccer" from every season except '2015/2016'

I think you are looking for this if statement:

if(($row['Season'] != "2015/2016") && ($row['Source'] == "xmlsoccer")){ 

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