简体   繁体   English

如果,否则,否则无法正常工作

[英]If, elseif and else not working like expected

I'm trying to check with if and elseif multiply $_sessions if they exist and if they have a fixed value. 我正在尝试检查是否和elseif乘以$ _sessions是否存在以及它们是否具有固定值。
My only problem is that the sessions all are in place and set to the expected value but my script doesn't go further then the second check. 我唯一的问题是,所有会话均已就位并设置为期望值,但我的脚本没有第二次检查更进一步。

So my question is here do i format the if, elseif, else not correctly or is it the way i check the session value's ? 所以我的问题是在这里我是否格式化if,elseif否则else格式不正确,还是我检查会话值的方式? And what should i do at a other way? 那我应该怎么做呢?

Script can be found here: http://pastebin.com/cpbCZJ0k 脚本可以在这里找到: http : //pastebin.com/cpbCZJ0k
It's 145 lines so i'm really not going to post here since it is so long and would take up the whole page. 这是145行,所以我真的不打算在这里发布,因为它很长,会占用整个页面。

Any help would be very appreciated. 任何帮助将不胜感激。

Expression from your second if isset($_SESSION['gedaanx']) && $_SESSION['gedaanx'] == 2 && isset($_SESSION['pre']) && $_SESSION['pre'] == 1 is also used in third one, so there are only two possibilities: script will execute second if or it'll just skip to else (because that expression returned false and third if will also fail). 第二个if isset($_SESSION['gedaanx']) && $_SESSION['gedaanx'] == 2 && isset($_SESSION['pre']) && $_SESSION['pre'] == 1也是在第三个脚本中使用,因此只有两种可能性: if脚本将执行第二个脚本if否则它将跳至else脚本(因为该表达式返回falseif表达式返回第三if失败)。

You have to reverse second and third if s. if s, if必须倒转第二和第三。

First of all, your code 首先,您的代码

if (isset($_SESSION['gedaanx']) && $_SESSION['gedaanx'] == 1 ){
...
elseif (isset($_SESSION['gedaanx']) && $_SESSION['gedaanx'] == 2 &&
       isset($_SESSION['pre']) && $_SESSION['pre'] == 1  ) {
...
elseif (isset($_SESSION['gedaanx']) && $_SESSION['gedaanx'] == 2 &&
       isset($_SESSION['pre']) && $_SESSION['pre'] == 1 &&
       isset($_SESSION['gedaan1']) && $_SESSION['gedaan1'] == 1 ) {
...
elseif (isset($_SESSION['gedaanx']) && $_SESSION['gedaanx'] == 2 &&
       isset($_SESSION['pre']) && $_SESSION['pre'] == 1 && 
       isset($_SESSION['gedaan1']) && $_SESSION['gedaan1'] == 1 && 
       isset($_SESSION['gedaan2']) && $_SESSION['gedaan2'] == 1) {
...

Now, notice that the first and second if clauses are incompatible, ie if the first is true the second will never be true and vise versa. 现在,请注意第一个和第二个if子句不兼容,即,如果第一个为true,则第二个永远不会为true,反之亦然。 That is a good case, your execution checks if the first clause is true, if it's true, the first branch is followed. 这是一个好例子,您的执行将检查first子句是否为true,如果为true,则遵循第一个分支。 If it's not true, the second clause is checked. 如果不正确,则检查第二个子句。 If it's true, the second branch is followed. 如果为真,则跟随第二个分支。

Note that if the first clause is true, the second one is not checked . 请注意,如果第一个子句为true, 则不选中第二个子句。 So for the case of third and fourth branches, if the first or second were true, no other branches will be followed, regardless of their if clauses. 因此,对于第三个和第四个分支,如果第一个或第二个为true,则不会跟随其他分支,无论其if子句如何。

However if the clause of the third/fourth branch is true, so is the clause of the second branch. 但是,如果第三/第四分支的子句为true,则第二分支的子句也是如此。 Thus the execution can never reach the third or fourth branch, always ending up in the second (or first) branch. 因此,执行永远无法到达第三或第四分支,总是在第二(或第一)分支中结束。

Remembering the note about incompatible clauses, you may want to make clauses incompatible, say, make the 2nd branch look like 记住关于不兼容子句的注释,您可能希望使子句不兼容,例如,使第二个分支看起来像

elseif (isset($_SESSION['gedaanx']) && $_SESSION['gedaanx'] == 2 &&
       isset($_SESSION['pre']) && $_SESSION['pre'] == 1 &&
       !isset($_SESSION['gedaan1'])  ) {
...

Or simply exchange the 4th branch and 2nd branch so that 4th is 2nd, 3rd is 3rd and 2nd is 4th. 或简单地交换第4个分支和第2个分支,使第4个为第2个,第3个为第3个,第2个为第4个。 Then the 4th branch will be checked immediately after the 1st. 然后将在第一个分支之后立即检查第四个分支。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM