elseif语句中的if语句导致错误

汤姆·威尔逊

好的,所以我以前有这段代码,它可以正常工作:

    $lastpost   = ForumPos::where('user_id', '=', Auth::id())->orderby('created_at', 'desc')->first();

    if ($validator->fails())
    {
            return Redirect::to('/forum/topic/'.$id.'/new')
                    ->withErrors($validator->messages());
    }
        elseif ($lastpost->created_at->diffInSeconds() < 15)
    {
            return Redirect::to('/forum/topic/'.$id.'/new')
                    ->withErrors('You really need to slow down with your posting ;)');
    }
        else
    {
            $new_thread             = new ForumThr;
            $new_thread->topic      = $id;
            $new_thread->user_id    = Auth::id();
            $new_thread->title      = Input::get('title');
            $new_thread->save();

            $new_post               = new ForumPos;
            $new_post->thread       = $new_thread->id;
            $new_post->user_id      = Auth::id();
            $new_post->body         = Input::get('body');
            $new_post->save();

            return Redirect::to('/forum/thread/'.$new_thread->id.'');
    }

一切正常,直到我注意到一个小问题,所以我不得不对此稍作改动以获取此信息:

    $hasposted  = ForumPos::where('user_id', '=', Auth::id())->count();

    if ($validator->fails()){
            return Redirect::to('/forum/topic/'.$id.'/new')
                    ->withErrors($validator->messages());
    } elseif ($hasposted != 0) {
        $last_post = ForumPos::where('user_id', '=', Auth::id())->orderBy('created_at', 'DESC')->first();

        if ($last_post->created_at->diffInSeconds() < 15) {
            return Redirect::to('/forum/topic/'.$id.'/new')
                    ->withErrors('You really need to slow down with your posting ;)');
        }
    } else {
            $new_thread             = new ForumThr;
            $new_thread->topic      = $id;
            $new_thread->user_id    = Auth::id();
            $new_thread->title      = Input::get('title');
            $new_thread->save();

            $new_post               = new ForumPos;
            $new_post->thread       = $new_thread->id;
            $new_post->user_id      = Auth::id();
            $new_post->body         = Input::get('body');
            $new_post->save();

            return Redirect::to('/forum/thread/'.$new_thread->id.'');
    }

现在,当我发布一个线程并进入elseif语句中的if语句时,我遇到了一个障碍。我收到以下错误:

错误#1

仅当我未在控制器中指定title变量时才出现此错误,因此视图可以获取它,但是不应有一个视图。有任何想法吗?:S

艾伦·斯托姆(Alan Storm)

看看您的elseif障碍物(第二种情况)

if(...)
{
    //first condition
    return ...;
}
elseif ($hasposted != 0) {
{
    //second condition    
    $last_post = ForumPos::where('user_id', '=', Auth::id())->orderBy('created_at', 'DESC')->first();

    if ($last_post->created_at->diffInSeconds() < 15) {
        return Redirect::to('/forum/topic/'.$id.'/new')
                ->withErrors('You really need to slow down with your posting ;)');
    }
} 
else
{
    //third condition
    return ...;        
}

当您的嵌套if语句失败时

$last_post->created_at->diffInSeconds() < 15

此块完成,其余条件条件完成,而不发出Redirect也就是说,您的嵌套if语句对第三个条件一无所知。PHP / Laravel正在执行您要求的操作-因此,请告诉它执行其他操作。

这纯粹是一种样式建议,但我已经达到了避免可能的多个分支条件的地步,尤其是从分支内部返回时。风格更像

if(...)
{
    return Redirect();  //...
}

if(...)
{
    return Redirect();  //...
}

if(...)
{
    return Redirect();  //...
}

if(...)
{
    return Redirect();  //...
}    

可能会在页面上看起来更长,但更清楚了发生了什么。

If this?  Do something and go away (`return`)

Still here? Well if this-other-thing then do something and go away (`return`)

**Still** here? Well if this-other-thing then do something and go away (`return`)    

您最终会思考一系列的是/否测试,并避免了嵌套条件逻辑遇到的非常人为/程序员的问题。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

PHP elseif语句中的额外{错误

来自分类Dev

If在If-elseif语句中不起作用

来自分类Dev

在 #if() #elseif() 语句中使用 #set() 指令

来自分类Dev

Switch语句中的错误

来自分类Dev

if语句中的错误

来自分类Dev

if else语句中的错误

来自分类Dev

LINQ语句中的错误

来自分类Dev

cmake IF 语句中的错误

来自分类Dev

Swift If 语句中的错误

来自分类Dev

在OCaml的assert语句中使用<>会导致错误

来自分类Dev

WHERE语句中的TSQL表变量导致错误

来自分类Dev

If-Then语句中的Python错误

来自分类Dev

在if语句中出现错误

来自分类Dev

if语句中缺少值错误

来自分类Dev

SUBSEQ语句中的错误答案

来自分类Dev

更新语句中的错误

来自分类Dev

if语句中的Python缩进错误

来自分类Dev

“For”语句中的 Excel Vba 错误

来自分类Dev

尝试与接收语句中的typedef值匹配会导致出现“错误的节点类型44”错误消息

来自分类Dev

if语句导致类型错误elm

来自分类Dev

if语句导致类型错误elm

来自分类Dev

批处理-If语句导致错误

来自分类Dev

在循环中的内联if语句中使用break会导致语法错误

来自分类Dev

在switch语句中定义块会导致编译器错误

来自分类Dev

为什么备用语法switch语句中的输出会导致语法错误

来自分类Dev

在循环中的内联if语句中使用break会导致语法错误

来自分类Dev

导致格式错误的“ Convert.ToInt”语句中的小数点?

来自分类Dev

三元运算符在 if 语句中导致错误

来自分类Dev

输入语句中的if语句