douzhanyan5015 2015-02-01 23:14
浏览 32
已采纳

尝试从函数中设置变量,并在同一行中检查它是否为false

I am trying to set a variable from a function, and then check if it is false in the same line. If it is false, I want to execute some code.

The code I have is the following:

  if ($wordid = wordnet_get_wordid_from_word($word) == FALSE) {
db_set_active();
    return $word;
  }

However it appears that $wordid is not getting set by this.

I've seen something like this being used before, and I've tested that inside the function, the value returned is NOT empty.

So what am I doing wrong here? Is it not possible to set a variable on the same line as you check whether it is false?

  • 写回答

2条回答 默认 最新

  • douquan9826 2015-02-01 23:22
    关注

    $wordis is getting set in this code. If you do an echo gettype($wordis) after the if you'll find it's a boolean. It's value will be the outcome of evaluating this: wordnet_get_wordid_from_word($word) == FALSE. Remember, if that evaluates false it'll skip the if statement anyway, that's probably why you think it's not working.

    If you want $wordid = wordnet_get_wordid_from_word($word) and to check if that's false, use brackets:

    if (($wordid = wordnet_get_wordid_from_word($word)) == FALSE) {..

    IMO, confusing code. Separate it out.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记