Nested if's not working in php either does not

2019-02-25 04:56发布

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

标签: php nested
2条回答
叛逆
2楼-- · 2019-02-25 05:14

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")
查看更多
Emotional °昔
3楼-- · 2019-02-25 05:35

Your IF statements are not doing what you think they are doing as that is not correct syntax for checking if a variable has more than one possible value. There are a few ways to do it but this might be the most concise:

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

becomes

if (in_array($req, array("remind", "tip", "half", "secHalf", "fin")))
查看更多
登录 后发表回答