|
Posted by Michael Fesser on 06/25/07 13:17
..oO(Janwillem Borleffs)
>Michael Fesser schreef:
>> In a situation like this a switch is the perfect solution:
>>
>> switch ($foo) {
>> case 1: doThis(); break;
>> case 2: doThat(); break;
>> case 3: doSomething(); break;
>> case 4: doSomethingElse(); break;
>> default: doWhatever();
>> }
>>
>> The alternative would be a cascade of 4 if-else statements ... Ugly.
>
>Like Geoff, I also favor the use of arrays. I wrote the following
>document some time ago on the subject:
>
>http://docs.jwscripts.com/PHP%20spaghetti.doc
I consider that a "C-style" hack. Of course you can use arrays like
that, but for me that's not what I consider readable code. IMHO it's not
really an array anymore, as it contains a lot of program logic. For me
an array is just a data container or something like a lookup table.
YMMV.
My own code for checkbox handling assigns each checkbox in a set a value
which is a power of 2. Then when receiving the form the checkbox values
are 'OR'ed together, so the final result is a simple integer. Of course
this could then be used as a key in an array (simple binary logic) if
necessary:
$mapping = array(
0 => 'a is not set b is not set c is not set',
1 => 'a is set b is not set c is not set',
2 => 'a is not set b is set c is not set',
3 => 'a is set b is set c is not set',
4 => 'a is not set b is not set c is set',
5 => 'a is set b is not set c is set',
6 => 'a is not set b is set c is set',
7 => 'a is set b is set c is set',
);
Micha
[Back to original message]
|