[英]If statement with and won't work?
一个非常愚蠢的问题,我是C#小子,我也在php中编程4年,但是有时当我在其中使用'and'或'&&'时如果声明不起作用。
例如当前的问题是这样的:
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>";
}
为什么
if(($row['Season'] != "2015/2016") && ($row['Source'] != "xmlsoccer")){
不行吗 我在数据库中有数百万行,并且我不想要2015/2016的xmlsoccer数据,但是当我运行它时,它什么也不返回。
当我只删除此&& ($row['Source'] != "xmlsoccer")
一切正常。
我100%确信一切都正确,例如当我转储时
var_dump($row['Season'] . " " . $row['Source']);
它返回例如'2013/2014''xmlsoccer',但if语句中没有内容...
请牢记Chasles关系。 为了说明,让我们做一些数学运算:
if(a && b)
可以关联到:
如果a和b为true,则结果为true
但是,如果您需要将其反转,您会想到:
if(not a and not b)
将被表示如下:
如果( a和b不正确,那么c为正确)
但是我们可以用Chasles定理翻译这最后一句话:
A + B = C
(A + B)\\ = C
因此,与Chasles关系,结果是
A\\ . B\\ = C
A\\ . B\\ = C
(点表示OR
)
对应于A || B = C
因此,我们可以说(A + B)\\ = C
等效于A\\ + B\\ = C
,这意味着在编程中:
! (a && b)
! (a && b)
为true等于!a || !b
!a || !b
也是如此
最后,您可以编写这两个相同的条件:
if( ! ( $row['Season'] == "2015/2016") && $row['Source'] == "xmlsoccer" ) )
if( $row['Season'] != "2015/2016") || $row['Source'] != "xmlsoccer" )
首先在SQL层上进行过滤,然后只返回您需要的内容。 其次,您明确要求所有不是xmlsoccer的内容
($row['Source'] != "xmlsoccer")
但是,当您转储看到“ 2013/2014”和“ xmlsoccer”的值并且不进入if语句时,您会感到困惑。 根据我对您提出的问题的了解,您希望每个季节都提供除“ 2015/2016”外所有属于“ xmlsoccer”的内容
我认为您正在寻找这个if语句:
if(($row['Season'] != "2015/2016") && ($row['Source'] == "xmlsoccer")){
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.