[英]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. 一个非常愚蠢的问题,我是C#小子,我也在php中编程4年,但是有时当我在其中使用'and'或'&&'时如果声明不起作用。
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. 我在数据库中有数百万行,并且我不想要2015/2016的xmlsoccer数据,但是当我运行它时,它什么也不返回。
When i remove just this && ($row['Source'] != "xmlsoccer")
everything works fine. 当我只删除此&& ($row['Source'] != "xmlsoccer")
一切正常。
I am 100% sure that is everything correct, for example when i dump 我100%确信一切都正确,例如当我转储时
var_dump($row['Season'] . " " . $row['Source']);
it returns for example, '2013/2014' 'xmlsoccer', but nothing inside if statement... 它返回例如'2013/2014''xmlsoccer',但if语句中没有内容...
Keep in mind the Chasles relation. 请牢记Chasles关系。 So to explain, let's do some mathematics : 为了说明,让我们做一些数学运算:
if(a && b)
can be associated in : if(a && b)
可以关联到:
if a and b is true so result is true 如果a和b为true,则结果为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(not a and not b)
将被表示如下:
if ( a and b is not true so c is true) 如果( a和b不正确,那么c为正确)
But we can translate this last sentence with the Chasles theorm that says : 但是我们可以用Chasles定理翻译这最后一句话:
A + B = C
(A + B)\\ = C
so with Chasles relation the result is 因此,与Chasles关系,结果是
A\\ . B\\ = C
A\\ . B\\ = C
(the dot means OR
) A\\ . B\\ = C
(点表示OR
)
Which corresponds to A || 对应于A || B = C B = C
So we can say that (A + B)\\ = C
is equivalent to A\\ + B\\ = C
which means in programming : 因此,我们可以说(A + B)\\ = C
等效于A\\ + B\\ = C
,这意味着在编程中:
! (a && b)
! (a && b)
is true is equivalent to !a || !b
! (a && b)
为true等于!a || !b
!a || !b
is also true !a || !b
也是如此
Finally you can write those 2 conditions that are the sames : 最后,您可以编写这两个相同的条件:
if( ! ( $row['Season'] == "2015/2016") && $row['Source'] == "xmlsoccer" ) )
if( $row['Season'] != "2015/2016") || $row['Source'] != "xmlsoccer" )
Firstly filter on the SQL layer, it should then return only what you need. 首先在SQL层上进行过滤,然后只返回您需要的内容。 Secondly you clearly ask for everything that isn't xmlsoccer 其次,您明确要求所有不是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. 但是,当您转储看到“ 2013/2014”和“ xmlsoccer”的值并且不进入if语句时,您会感到困惑。 From what I understand from your question you want everything that is "xmlsoccer" from every season except '2015/2016' 根据我对您提出的问题的了解,您希望每个季节都提供除“ 2015/2016”外所有属于“ xmlsoccer”的内容
I think you are looking for this if statement: 我认为您正在寻找这个if语句:
if(($row['Season'] != "2015/2016") && ($row['Source'] == "xmlsoccer")){
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.