brentman Posted September 21, 2013 Share Posted September 21, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/282332-super-nested-ifs-better-as-switches-multiple-logic-expressions/ Share on other sites More sharing options...
brentman Posted September 21, 2013 Author Share Posted September 21, 2013 Forgot to mention some user inputs are this OR this so a single case or single if statement would need like x AND x AND x AND ( a OR b) AND etc Quote Link to comment https://forums.phpfreaks.com/topic/282332-super-nested-ifs-better-as-switches-multiple-logic-expressions/#findComment-1450517 Share on other sites More sharing options...
LunarIsSexy Posted September 21, 2013 Share Posted September 21, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/282332-super-nested-ifs-better-as-switches-multiple-logic-expressions/#findComment-1450518 Share on other sites More sharing options...
DFulg Posted September 21, 2013 Share Posted September 21, 2013 Include all of the falsy logic in one statement: if ( $number == 5 || $letter == 'a' || $option > 2 ) { //false block } else { // true block } much more readable and logical Quote Link to comment https://forums.phpfreaks.com/topic/282332-super-nested-ifs-better-as-switches-multiple-logic-expressions/#findComment-1450520 Share on other sites More sharing options...
brentman Posted September 21, 2013 Author Share Posted September 21, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/282332-super-nested-ifs-better-as-switches-multiple-logic-expressions/#findComment-1450524 Share on other sites More sharing options...
kicken Posted September 21, 2013 Share Posted September 21, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/282332-super-nested-ifs-better-as-switches-multiple-logic-expressions/#findComment-1450530 Share on other sites More sharing options...
brentman Posted September 21, 2013 Author Share Posted September 21, 2013 (edited) 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 September 21, 2013 by brentman Quote Link to comment https://forums.phpfreaks.com/topic/282332-super-nested-ifs-better-as-switches-multiple-logic-expressions/#findComment-1450548 Share on other sites More sharing options...
vinny42 Posted September 21, 2013 Share Posted September 21, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/282332-super-nested-ifs-better-as-switches-multiple-logic-expressions/#findComment-1450551 Share on other sites More sharing options...
brentman Posted September 22, 2013 Author Share Posted September 22, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/282332-super-nested-ifs-better-as-switches-multiple-logic-expressions/#findComment-1450718 Share on other sites More sharing options...
.josh Posted September 22, 2013 Share Posted September 22, 2013 can't even begin to give you a solid answer without solid details, but generally speaking, database will usually always win out on efficiency in terms of selecting stuff. Quote Link to comment https://forums.phpfreaks.com/topic/282332-super-nested-ifs-better-as-switches-multiple-logic-expressions/#findComment-1450739 Share on other sites More sharing options...
vinny42 Posted September 23, 2013 Share Posted September 23, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/282332-super-nested-ifs-better-as-switches-multiple-logic-expressions/#findComment-1450758 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.