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. 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 ? And what should i do at a other way?

Script can be found here: 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.

Any help would be very appreciated.

Answers


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).

You have to reverse second and third ifs.


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, i.e. if the first is true the second will never be true and vise versa. That is a good case, your execution checks if the first clause is true, if it's true, the first branch is followed. 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. 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.

However if the clause of the third/fourth branch is true, so is the clause of the second branch. 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. Then the 4th branch will be checked immediately after the 1st.


Need Your Help

MVC - View will not display

c# asp.net-mvc razor model-view-controller

I have a view I cannot get to display in MVC

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.