Jaguar Posted June 11, 2007 Share Posted June 11, 2007 Which is faster else if's or switch? I find conflicting answers. Also what would be the prefered way to write this, switch($_POST['action']) { case 'add': if($error) { header("Location:add.php?message=$error"); } else { mysql_query(); header('Location:add.php?message=Success!'); } break; default: header('Location:add.php'); break; } switch($_POST['action']) { case 'add' && $error: header("Location:add.php?message=$error"); break; case 'add': mysql_query(); header("Location:add.php?message=Success!"); break; default: header("Location:add.php"); break; } if($_POST['action'] === 'add') { if($error) { header("Location:add.php?message=$error"); } else { mysql_query(); header('Location:add.php?message=Success!'); } } else { header('Location:add.php'); } if($_POST['action'] === 'add' && $error) { header("Location:add.php?message=$error"); } else if($_POST['action'] === 'add') { mysql_query(); header('Location:add.php?message=Success!'); } else { header('Location:add.php'); } The last one in this instance looks easiest to read to me. However, in my actual code there are dozens of actions and using the last method it becomes a little hard to read. In my code I find the first way easiest to read. Is using if's inside a switch bad form? Link to comment https://forums.phpfreaks.com/topic/55120-best-way-with-switch-if-or-nested-ifs/ Share on other sites More sharing options...
Wildbug Posted June 11, 2007 Share Posted June 11, 2007 Make a test case for each, loop it a bunch of times, and record benchmarks if you need to optimize for performance. I don't think they're much different. The only time you might need to choose between them for performance reasons is (a) you have way too many of the statements in your program (I mean WAY too many), (b) you have a REALLY, REALLY busy site, or © your computer sucks so bad it gets bogged down on an if statement. I'd prefer the switch statement. To make the code easier to read, you might make the following formatting adjustments: - Use commented dashes to visually highlight cases. - Use single line if-else, if you can. switch($_POST['action']) { // ---------------------------------------------- case 'add': // ---------------------------------------------- if ($error) header("Location:add.php?message=$error"); else mysql_query() && header('Location:add.php?message=Success!'); break; // ---------------------------------------------- case 'other': // ---------------------------------------------- header("Location:add.php?message=" . $error ? $error : 'Success!'); exit; // ---------------------------------------------- default: // ---------------------------------------------- header("Location: add.php"); } I don't know how your real code is organized, but can you move that if($error) bit outside the switch, preceeding it or something? Link to comment https://forums.phpfreaks.com/topic/55120-best-way-with-switch-if-or-nested-ifs/#findComment-272518 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.