沃梦达 / 编程问答 / php问题 / 正文

嵌套如果在 php 中不起作用,要么什么也不做,要么到代码的末尾

Nested if#39;s not working in php either does nothing or goes to the end of the code(嵌套如果在 php 中不起作用,要么什么也不做,要么到代码的末尾)

本文介绍了嵌套如果在 php 中不起作用,要么什么也不做,要么到代码的末尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经对这段代码感到沮丧几个小时了,我只需要鞠躬寻求帮助.我试图让这些嵌套的 if 工作,但它要么什么都不返回,我也没有收到任何错误,要么它会在编码结束时它应该事先停止.这些输入都来自组单选按钮,顺序的原因是,一旦到达被调用的函数,它们就会从最简单的到更复杂的过滤器.我首先尝试了嵌套 if,这就是我的结果,然后也尝试了 switch 并与它在同一个地方结束.我在这里:

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;
                    }

            }
    }
}   

我知道迭代的最后一个if"不应该是else if",但这是因为否则我会在 if 参数下收到意外{"的语法错误.我也尝试过类似于最后的 2 个if",我只是为它们全部放置 if 并且没有嵌套它们,但这确实通过第二个 if 语句起作用,然后它每次都走到最后并绕过所有其他可能性.

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

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
                    }
                }
        }
    }

推荐答案

试试这个,

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

而不是(如 AD7six 所述 - 无意/不合逻辑)

instead of (As AD7six stated - unintentional/illogical)

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

这篇关于嵌套如果在 php 中不起作用,要么什么也不做,要么到代码的末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:嵌套如果在 php 中不起作用,要么什么也不做,要么到代码的末尾

基础教程推荐