PHP 如果变量=a而不是=b

问题描述 投票:0回答:1

php 7.3.18

mysql 5.7.30

下面是相关代码,马上就失败了,所以希望能得到纠正。

第一个'if'没问题,但问题出在第二个+第三个。

$targetcatid2、$targetcatid3、$targetweight2、$targetweight3已经正确声明。

目的是如果一个产品在Category2而不在Category3,那么重量就会变成Weight2and vice-a-versa。

$row2=mysqli_fetch_array($select2);

if($row2['category_id']== $targetcatid){
    $row['product_weight']= $targetweight;
}

if($row2['category_id']== $targetcatid2 && !== $targetcatid3){
    $row['product_weight']= $targetweight2;
}

if($row2['category_id']== $targetcatid3 && !== $targetcatid2){
    $row['product_weight']= $targetweight3;
}

我最近的努力是

if($row2['category_id']== $targetcatid){$row['product_weight']= $targetweight; }。

if($row2['category_id']==$targetcatid2 && $row2['category_id']!==$targetcatid3){$row['product_weight']=$targetweight2; }。

elseif($row2['category_id']==$targetcatid3 && $row2['category_id']!==$targetcatid2){$row['product_weight']=$targetweight3; }。

elseif($row2['category_id']==$targetcatid2 && $row2['category_id']==$targetcatid3){$row['product_weight']=$targetweight3; }。

我发现新的条件不工作,即使是单独的,但不明白为什么。

elseif($row2['category_id'] == $targetcatid2 && $row2['category_id'] == $targetcatid3){ $row['product_weight'] = $targetweight3; }。

php if-statement comparison
1个回答
0
投票

你没有正确理解&&运算符。

取而代之的是

if($row2['category_id']== $targetcatid2 && !== $targetcatid3){

你应该分别比较条件。

if($row2['category_id']== $targetcatid2 && $row2['category_id'] !== $targetcatid3){
© www.soinside.com 2019 - 2024. All rights reserved.