doutan5337 2013-05-12 03:58
浏览 79

PHP函数中的return语句[重复]

This question already has an answer here:

I'm having some problems with a Return statement in PHP. The thing is that, no matter what happend inside my function I always get a false value out of the function. I really think that is because of the Return statement because I try to use it in other functions and I don't get a diferent value.

public function valid_token ()
{
    if (!isset($_SESSION['token']) || $this->token != $_SESSION['token'])
    {
        $this->errors[] = "Formulario incorrecto";
    }

return count($this->errors)? 0 : 1;
}

Out of this function I always get a false value (0). The same happends when i call:

public function valid_data () 
{
    if (empty($this->sectorName) || empty($this->sectorInfo) || empty($this->sectorCat))
    {
        $this->errors [] = "Datos incorrectos";
    }

return count($this->errors)? 0 : 1;
}

Of course, I call both functions when I have already sent the form and have set the token.

</div>
  • 写回答

2条回答 默认 最新

  • douhuang75397 2013-05-12 03:59
    关注

    Because you are just counting not checking,Try this

    return count($this->errors) > 0 ? 0 : 1;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错