douzhang3898 2009-12-03 22:33
浏览 64
已采纳

if语句用于确定函数返回的问题

Im using an if statement to determine what to return in a function, but it seems to be not working the way i want it to.

function DoThis($dogs, $cats){
// do something with dogs, pet them perhaps.

$reg = $dogs[0];
$nate = $dogs[1];

if($cats = "dave"){return $reg;}
if($cats = "tom"){return $nate;}

}

$cats is a string (if that helps), and when entered it doesn't yield any return. If i manually set a return, that works, but the above doesnt for some reason.

  • 写回答

3条回答 默认 最新

  • dongzha3058 2009-12-03 22:35
    关注

    To test for equality, use the == (double equals) operator instead of the = (single equals) operator.

    For example:

    if("dave" == $cats){return $reg;}
    if("tom"  == $cats){return $nate;}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 7-1 jmu-java-m02-使用二维数组存储多元线性方程组
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题