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

PHP 7.3.18


MySQL 5.7.30


这是相关的代码,它立即失败,所以我很感激它被纠正。


第一个“如果”没问题,但问题出在第二个和第三个。


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


目的是如果产品属于 Category2 而不是 Category3,则重量将变为 Weight2,反之亦然。


$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;

}

我最近的努力是:


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


如果($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; }


慕莱坞森
浏览 108回答 1
1回答

HUX布斯

您没有正确理解 && 运算符。而不是这个if($row2['category_id']== $targetcatid2 && !== $targetcatid3){您应该分别比较条件。if($row2['category_id']== $targetcatid2 && $row2['category_id'] !== $targetcatid3){
打开App,查看更多内容
随时随地看视频慕课网APP