php - 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;
}
我最近的努力是:
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; }
解决方案
您没有正确理解 && 运算符。
而不是这个
if($row2['category_id']== $targetcatid2 && !== $targetcatid3){
您应该单独比较条件。
if($row2['category_id']== $targetcatid2 && $row2['category_id'] !== $targetcatid3){
推荐阅读
- ssl - Windows 10 设备门户预配自签名 SSL 证书
- r - 用 R 中的向量替换 data.frame 的一些值
- ios - Receigen Xcode 10.1 - 未知类型名称'UIDevice'
- node.js - 浏览器/防病毒软件可以阻止对直接 IP 地址而不是域名的请求吗?
- c++ - 功能定义不允许 RetailItem
- dialogflow-es - 使用 Dialogflow 时,后续意图与在父意图中需要参数有什么区别?
- kotlin - 如何在 Workspace 标题中创建搜索字段?
- c++11 - 将节点(包含 str 和 bool 值)插入数组 C++
- sql - 将列名声明为变量
- java - 使用 Jackson GWT 访问嵌套的 JSON 值