Jump to content

Super nested ifs - Better as switches? Multiple logic expressions


Recommended Posts

Here is my example:

//User Inputs
$number
$letter
$option 1,2,3,4, or 5
$true or false
etc
Like 10 different things

//Here starts the if
if ($number = 5) {
//not allowed
}
else {
     if ($letter == a) {
     //not allowed
     }
     else {
          if ($option > 2) {
          //not allowed
          }
         else {
         ...... nested through all options
         echo "ALLOWED";
          }
     }
}

If i do it with switches it would need to be like:

 

switch

case "$number !=5 AND $letter != 'a' AND $option <= 2 AND etc etc" {

}

 

But I tried this first and was having problems logically making it function correctly because there are so many variables to fit in there correctly.

 

Any suggestions?

 

 

A quicker and much neater way to do it would be like this atleast in my opinion it is.

if ($number = 5) {
//not allowed
}
else if ($letter == a) {
     //not allowed
     }
else if ($option > 2) {
     //not allowed
     }
else {
     echo "ALLOWED";
     }
     }
}

The way you're doing it is just fine and it really depends on which way is easier for you to understand. Its all about preference as both of them will give you the same output.

A quicker and much neater way to do it would be like this atleast in my opinion it is.

if ($number = 5) {
//not allowed
}
else if ($letter == a) {
     //not allowed
     }
else if ($option > 2) {
     //not allowed
     }
else {
     echo "ALLOWED";
     }
     }
}

The way you're doing it is just fine and it really depends on which way is easier for you to understand. Its all about preference as both of them will give you the same output.

 

This isn't a problem if multiple elseif's are satisified? Could I use a conditional switch like this instead? The reason I don't like mine made of nested if/elses is because i believe its slow and all my ifs are actually just to redirect people to the next page of my site, literally every click is going through this so saving resources is important. Plus its true its a mess. I made a change and it took me awhile to sort it out.

This isn't a problem if multiple elseif's are satisified?

Only the first true condition in a series of if/else if/else statements will have it's statements executed. All the rest of the branches will be skipped. It's just a nice way to write:

if (a){
  .
} else {
  if (b){
     .
  } else { 
    if (c){
       .
    } else {
      if (d){
         .
      }
      else {}
    }
  }
}
What exactly are you trying to do with all these conditions? There may be a better way that doesn't involve so many if's.

There will be between aprox 10 and 500 records in db. The user will fill in about 10 options. I need to get all records that fit the requirements of the user and then "randomly" select one but sometimes the options will be weighted depending on specific settings in the db records. The db records are also user inputted.

 

I found a nice weighted random selector function but I need to get the "Allowed" DB Records in an array.

 

SO, this portion of code that is currently if's is in a loop and upon meeting all requirements it gets added to the array.

Edited by brentman

So you select all records from the database and loop through them to see which match the criteria.

 

Why don't you just build a query that get's only the records that meet the criteria?

 

What kind of criteria d you have?

So you select all records from the database and loop through them to see which match the criteria.

 

Why don't you just build a query that get's only the records that meet the criteria?

 

What kind of criteria d you have?

 

I am not that great with mysql so I have not considered it. But in terms of system resources, better to do a complex pull from database or to pull all data and sort it out in php? I generally figure to use the DB as little as possible is best but I am sure there would be exceptions.

If the application is to filter records then yes, the database will win. The reason is that databases can use indexes to very quickly filter out records that have no chance of making it to the final set.

Think of a telephony directory; if you are looking for the phonenumber of a person who'se name begins with the letter 'H', you can skip all the pages a-g and once you have looked through al he 'H' you can stop because app the pages after tht won't contain any more names starting with 'H'. The database also knows statistics about how many records there are for each of the indexes so it can work out which filters to applu in which order so that a few records as possible have to be processed.

 

Alltogether you'd have to have one flipping fast PHP routine if you can process all the records in what is basically one big sequential scan, faster than the database can eliminate records by not even looking at them.

 

Perhaps it's wise to post an example of the sort of data you have and the type of filtering that you want to do.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.