[thelist] php SWITCH vs ELSE IF

Christian Heilmann codepo8 at gmail.com
Mon Apr 24 11:00:52 CDT 2006


> Well, one difference is that your example will execute the is_page()
> function up to seven times while the "switch" statement would execute a
> similar function just once. If is_page() is a costly function, it could
> affect performance noticeably.  I suspect all your function does is to
> compare some query string value with the parameter supplied--that
> wouldn't be very costly at all.
>
> I prefer the "switch" statement over "else if" when the method of
> evaluation is the same in each case, such as in your example. Notice
> that is_page() would need to be re-written to return the 'type' of page.
>
> switch(get_page_type()){
> case 'miscellaneous':
>     echo $misc;
>     break;
> case 'associates':
>     echo $misc;
>     break;
> //fill in the rest here...
> default:
>     die('unknown page type');
>     break;
> }
>
> I might use the "else if" construct if the evaluations are different in
> each step, but then "nested ifs" are often easier to read.

I'd store the navigation items in an array and use

echo $misc[get_page_type()];

:-)



--
Chris Heilmann
Blog: http://www.wait-till-i.com
Writing: http://icant.co.uk/
Binaries: http://www.onlinetools.org/



More information about the thelist mailing list