duansanzi5265 2012-03-28 14:10
浏览 39
已采纳

PHP &&或if else语句无法正常工作

So I have this survey that I am pulling results from and I have to basically make a bunch of if...else if... statements involving which feedback is submitted.

I have set it up to work correctly...except for one problem. I know it's probably something stupid, but for the life of me I can't figure it out.

Anyways here is the code.

if (($QID_A == 23) && ($row_result_2['Feedback'] == "")) {
    echo "";
}
else if (($QID_A == 23) && ($row_result_2['Feedback'] <= 3)) {
    echo $row_custom['Content'];
}
else if (($QID_A == 23) && ($row_result_2['Feedback'] == 4) || ($row_result_2['Feedback'] == 5) || ($row_result_2['Feedback'] == 6)) {
    echo $row_custom['Content2'];
}
else if (($QID_A == 23) && ($row_result_2['Feedback'] >= 7)) {
    echo $row_custom['Content3'];   
}

The problem I am getting is if $row_result_2['Feedback'] is == to 5 or 6. It just repeats the line over and over again, basically failing to evaluate. It works fine for 4, so I am thinking my problem is somewhere in how I set up the || statements?

Any help would be appreciated. Thank you.

While I'm at it would there be a simpler way to write this statement? So i don't have to copy it for each QID_A field 1-50?

UPDATE:: At this point I am at a loss as to why none of the below solutions are working.. I think this might be a deeper issue as the results are still repeating 24 times... Being that QID_A is 23 I think this might be related.. but I can't seem to find a connection... Thanks again for the help though guys I appreciate it.

Just for the hell of it I tried writing the statements out individually.... same result... for example.

if (($QID_A == 22) && ($row_result_2['Feedback'] == "")) {
echo "";
 }
 else if (($QID_A == 22) && ($row_result_2['Feedback'] <= 3)) {
  echo $row_custom['Content'];
  }
  else if (($QID_A == 22) && ($row_result_2['Feedback'] == 4)) {
  echo $row_custom['Content2'];
 }
 else if (($QID_A == 22) && ($row_result_2['Feedback'] == 5)) {
  echo $row_custom['Content2'];
 }
 else if (($QID_A == 22) && ($row_result_2['Feedback'] == 6)) {
  echo $row_custom['Content2'];
 }
 else if (($QID_A == 22) && ($row_result_2['Feedback'] >= 7)) {
  echo $row_custom['Content3']; 
 }
  • 写回答

7条回答 默认 最新

  • duanshangying5102 2012-03-28 14:22
    关注

    You need an extra pair of brackets to make it obvious what the precedence should be with your operators (and you don't need the brackets round each expression):

    else if ($QID_A == 23 && ($row_result_2['Feedback'] == 4 || $row_result_2['Feedback'] == 5 || $row_result_2['Feedback'] == 6)) {
    

    echo $row_custom['Content2']; }

    But you might be better off writing this logic use a case so it's clearer, i.e.

    if ($QID_A == 23) {
        switch( $row_result_2['Feedback'] ) {
          case '':
            echo '';
            break;
          case 0:
          case 1:
          case 2:
          case 3:
            echo $row_custom['Content'];
            break;
          case 4:
          case 5:
          case 6:
            echo $row_custom['Content2'];
            break;
          default:
            echo $row_custom['Content3']; 
            break;
       }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。