如果无法在php中嵌套,则无作用或进入代码末尾 [英] Nested if's not working in php either does nothing or goes to the end of the code

查看:59
本文介绍了如果无法在php中嵌套,则无作用或进入代码末尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个小时以来,我一直对这段代码感到沮丧,我只好低头寻求帮助.我试图让这些嵌套的if起作用,但是它要么什么都不返回,没有错误,或者将要在编码的末尾提前停止.这些输入全部来自组单选按钮,其顺序的原因是,一旦到达要调用的功能,它们便会从最简单的过滤到更复杂的过滤.我一开始尝试嵌套if,这是我的结果,然后尝试切换并用它缠绕在同一位置.这是我的位置:

if ($req === "remind" || "tip" || "half" || "secHalf" || "fin")
{
    //some code;
}
else 
{
if ($manUpdate === "first15" || "first10" || "first5" || "sec15" || "sec10" || "sec5")
    {
        //some code;
    }
else
    {
        if ($TypeA === "free throw.")
            {


                if($TypeA === "free throw." && $PointA === "1")
                    {
                        //some code;
                    }
                else if($TypeA === "free throw." && $PointA === "2")
                    {
                        //some code;
                    }
                else if($TypeA === "free throw." && $PointA === "3")
                    {
                        //some code;
                    }
            }
        else if ($TypeB === "free throw.")
            {
                if($TypeB === "free throw." && $PointB === "2")
                    {
                        //some code;
                    }
                else if($TypeB === "free throw." && $PointB === "2")
                    {
                        //some code;
                    }
                else if($TypeB === "free throw." && $PointB === "3")
                    {
                        //some code;
                    }
            }
        else
            {
                if ($TypeA === "shot." || "Slam Dunk!" || "from Downtown!")
                    {
                        //some code;
                    }

                if ($TypeB === "shot." || "Slam Dunk!" || "from Downtown!")
                    {
                        //some code;
                    }

            }
    }
}   

我知道迭代的最后一个"if"不应该是"else if",而是因为否则我将在if参数下得到意外的"{"的语法错误.我还尝试了类似于最后2个"if"的方法,在该方法中,我只是为所有对象添加了if,并且没有嵌套它们,但这确实在第二个if语句中起作用,然后每次都到最后并绕过所有其他可能性.

我还知道每个语句中的代码都可以工作,因为当在自己的页面上单独尝试时,它们可以按预期运行,因此没有理由将所有多余的代码都放在其中.

任何帮助都将不胜感激,因为我以前没有处理过嵌套的问题,这似乎是我认为应该的,但当然,如果我们都知道我们不需要询问的所有内容吗?预先感谢您浏览并提供任何帮助!

编辑因此,这是下面列出的工作代码,但是我遇到了一个新问题.似乎我可以运行1个代码实例,但是它可以正常工作,但是当我将文件夹中的一组新文件复制到另一个文件并运行它时,内部代码现在已损坏,而与此链接的页面却无法执行任何事物.它的行为好像一切都正常工作,并且日志中没有任何php错误.想知道是否需要更改会话名称,因为它们在两组文件中都相同,但是它们位于2个单独的文件夹中,因为最终我打算同时运行此脚本的5个实例.

if ($req === "remind" || $req === "tip" || $req === "half" || $req === "secHalf" || $req === "fin")
{
    ManualBasic();
}
else 
{
if ($manUpdate === "first15" || $manUpdate === "first10" || $manUpdate === "first5" || $manUpdate === "sec15" || $manUpdate === "sec10" || $manUpdate === "sec5")
    {
        Manual510();
    }
else
    {
        if ($TypeA === "free throw.")
            {


                if($TypeA === "free throw." && $PointA === "1")
                    {
                        // some code
                    }
                else if($TypeA === "free throw." && $PointA === "2")
                    {
                        // some code
                    }
                else if($TypeA === "free throw." && $PointA === "3")
                    {
                        // some code
                    }
            }
        else if ($TypeB === "free throw.")
            {
                if($TypeB === "free throw." && $PointB === "1")
                    {
                        // some code
                    }
                else if($TypeB === "free throw." && $PointB === "2")
                    {
                        // some code
                    }
                else if($TypeB === "free throw." && $PointB === "3")
                    {
                        // some code
                    }
            }
        else
            {
                if ($TypeA === "shot.")
                    {
                    // some code
                    }
                if ($TypeA === "Slam Dunk!")
                    {
                    // some code
                    }
                if ($TypeA === "from Downtown!")
                    {
                    // some code
                    }
                if ($TypeB === "shot.")
                    {
                    // some code
                    }
                if ($TypeB === "Slam Dunk!")
                    {
                    // some code
                    }
                if ($TypeB === "from Downtown!")
                    {
                        // some code
                    }
                }
        }
    }

解决方案

尝试一下,

if ($req === "remind" || $req === "tip" || $req === "half" || $req === "secHalf" || $req === "fin")

代替(如AD7six所述-无意/不合逻辑)

if ($req === "remind" || "tip" || "half" || "secHalf" || "fin")

I have been getting frustrated with this code for hours now and I just have to bow and ask for some help. I am trying to get these nested if's to work but it is either returning nothing and I am getting no errors or it is going to the end of the coding where it should have stopped beforehand. These inputs are all from groups radio buttons and the reason for the order is so that they would filter through from the simplest to the more complex once it gets to the function that is being called. I tried nested if's at first and that was my result and then tried switch as well and wound up in the same place with it. Here is where I am:

if ($req === "remind" || "tip" || "half" || "secHalf" || "fin")
{
    //some code;
}
else 
{
if ($manUpdate === "first15" || "first10" || "first5" || "sec15" || "sec10" || "sec5")
    {
        //some code;
    }
else
    {
        if ($TypeA === "free throw.")
            {


                if($TypeA === "free throw." && $PointA === "1")
                    {
                        //some code;
                    }
                else if($TypeA === "free throw." && $PointA === "2")
                    {
                        //some code;
                    }
                else if($TypeA === "free throw." && $PointA === "3")
                    {
                        //some code;
                    }
            }
        else if ($TypeB === "free throw.")
            {
                if($TypeB === "free throw." && $PointB === "2")
                    {
                        //some code;
                    }
                else if($TypeB === "free throw." && $PointB === "2")
                    {
                        //some code;
                    }
                else if($TypeB === "free throw." && $PointB === "3")
                    {
                        //some code;
                    }
            }
        else
            {
                if ($TypeA === "shot." || "Slam Dunk!" || "from Downtown!")
                    {
                        //some code;
                    }

                if ($TypeB === "shot." || "Slam Dunk!" || "from Downtown!")
                    {
                        //some code;
                    }

            }
    }
}   

I know that the last "if" of the iteration is not supposed to be "else if" but it is because otherwise I get the syntax error for the unexpected "{" under the if parameters. I also have tried it similar to the final 2 "if's" where I just put if for them all and didn't nest them but that does work through the second if statement then it just goes to the end each time and bypasses all of the other possibilities.

I also know that the code within each of the statements work because when tried individually on their own pages they do function as intended so there was no reason to throw all that extra coding in there.

Any help is greatly appreciated as I haven't dealt with nesting if's further than just one inside the other before and this appears as I think it should but of course if we all knew everything we wouldn't need to ask? Thank you in advance for taking a look and any help that you can offer!

EDIT So here is the working code as listed below but I have ran into a new issue. It seems that I can run 1 instance of the code but it works beautifully but when I copy a new set of files in the folder to another file and run it the code inside is now broken and while the page that this links to fails to do anything. It acts as if everything is working properly and I am not getting any php errors in the log. Wondering if I need to change the session names due them being the same in both sets of files but they are in 2 separate folders as ultimately I intend to have 5 instances of this script running simultaneously.

if ($req === "remind" || $req === "tip" || $req === "half" || $req === "secHalf" || $req === "fin")
{
    ManualBasic();
}
else 
{
if ($manUpdate === "first15" || $manUpdate === "first10" || $manUpdate === "first5" || $manUpdate === "sec15" || $manUpdate === "sec10" || $manUpdate === "sec5")
    {
        Manual510();
    }
else
    {
        if ($TypeA === "free throw.")
            {


                if($TypeA === "free throw." && $PointA === "1")
                    {
                        // some code
                    }
                else if($TypeA === "free throw." && $PointA === "2")
                    {
                        // some code
                    }
                else if($TypeA === "free throw." && $PointA === "3")
                    {
                        // some code
                    }
            }
        else if ($TypeB === "free throw.")
            {
                if($TypeB === "free throw." && $PointB === "1")
                    {
                        // some code
                    }
                else if($TypeB === "free throw." && $PointB === "2")
                    {
                        // some code
                    }
                else if($TypeB === "free throw." && $PointB === "3")
                    {
                        // some code
                    }
            }
        else
            {
                if ($TypeA === "shot.")
                    {
                    // some code
                    }
                if ($TypeA === "Slam Dunk!")
                    {
                    // some code
                    }
                if ($TypeA === "from Downtown!")
                    {
                    // some code
                    }
                if ($TypeB === "shot.")
                    {
                    // some code
                    }
                if ($TypeB === "Slam Dunk!")
                    {
                    // some code
                    }
                if ($TypeB === "from Downtown!")
                    {
                        // some code
                    }
                }
        }
    }

解决方案

Try this,

if ($req === "remind" || $req === "tip" || $req === "half" || $req === "secHalf" || $req === "fin")

instead of (As AD7six stated - unintentional/illogical)

if ($req === "remind" || "tip" || "half" || "secHalf" || "fin")

这篇关于如果无法在php中嵌套,则无作用或进入代码末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆