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 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名