Hello,
I have a number of scripts that are not very long, and it would be much simpler if i could have these in the one script and pass some paramater to the script to choose which part I want to use. The use of multiple IF statements seems inefficient
any ideas?
1 script - multiple operations?
Moderator: General Moderators
-
CrazyJimmy
- Forum Commoner
- Posts: 34
- Joined: Tue Nov 19, 2002 1:40 pm
-
CrazyJimmy
- Forum Commoner
- Posts: 34
- Joined: Tue Nov 19, 2002 1:40 pm
I'm not sure if switching is any faster than IF-ing - could be wrong. I think the key to making either optimal is to put the most frequently called case first, second next etc (even then it's probably only saving microseconds).
Using a GET var to switch / case on is fine.
Of course, anyone could edit the URL to call any function in the switch statement, at any time. Suppose one of the cases contains an admin-level function: you'd have to make sure to authenticate the user beforehand (another IF!) - but then you'd be doing that anyway, right?
Also, query string tampering could call functions in a "wrong" order: would that mess things up? Look at how your code would cope. Maybe a forum "new poll" script expects a new topic to be created first, for example
The final possibility (query string tampering which creates an undefined switch case value) would call the "default" case, if there is one (I think - never actually tested this).
Using a GET var to switch / case on is fine.
Of course, anyone could edit the URL to call any function in the switch statement, at any time. Suppose one of the cases contains an admin-level function: you'd have to make sure to authenticate the user beforehand (another IF!) - but then you'd be doing that anyway, right?
Also, query string tampering could call functions in a "wrong" order: would that mess things up? Look at how your code would cope. Maybe a forum "new poll" script expects a new topic to be created first, for example
The final possibility (query string tampering which creates an undefined switch case value) would call the "default" case, if there is one (I think - never actually tested this).