switch easy

Hi all,

What is the proper syntax (if exists) for:

switch( $myswitch ){
case "str1" | "str2" | "str3" | "str4":
$_th = "user/".$url."/_thumb/".$file;
break;

default:
$_th = $gfx."/file.gif";
}


U understand that i want to do the same thing in case str1,str2,str3..

I try also {case "str1" || "str2" || "str3" || "str4":}

It doesn't work.



2u.
severin [ So, 24 Juni 2007 21:27 ] [ ID #1747370 ]

Re: switch easy

..oO(severin)

>What is the proper syntax (if exists) for:
>
>switch( $myswitch ){
> case "str1" | "str2" | "str3" | "str4":
> $_th = "user/".$url."/_thumb/".$file;
> break;
>
> default:
> $_th = $gfx."/file.gif";
>}

switch ($myswitch) {
case 'str1':
case 'str2':
case 'str3':
case 'str4':
$_th = "user/$url/_thumb/$file";
break;
default:
$_th = "$gfx/file.gif";
}

If there are just these two possible results, then you could also do it
with a simple in_array() check and a ternary operator (a shortcut for an
if-then-else statement):

$strings = array('str1', 'str2', 'str3', 'str4');
$_th = in_array($myswitch, $strings)
? "user/$url/_thumb/$file"
: "$gfx/file.gif";

Micha
Michael Fesser [ So, 24 Juni 2007 21:37 ] [ ID #1747371 ]

Re: switch easy

Thank you for your answer

I have many different extention to deal with and i will use switch
rather than ternary operator.

but i will keep in mind the > $_th = in_array($myswitch, $strings)
which may be usefull.

thx.


Michael Fesser wrote:
> .oO(severin)
>
>
>>What is the proper syntax (if exists) for:
>>
>>switch( $myswitch ){
>> case "str1" | "str2" | "str3" | "str4":
>> $_th = "user/".$url."/_thumb/".$file;
>> break;
>>
>> default:
>> $_th = $gfx."/file.gif";
>>}
>
>
> switch ($myswitch) {
> case 'str1':
> case 'str2':
> case 'str3':
> case 'str4':
> $_th = "user/$url/_thumb/$file";
> break;
> default:
> $_th = "$gfx/file.gif";
> }
>
> If there are just these two possible results, then you could also do it
> with a simple in_array() check and a ternary operator (a shortcut for an
> if-then-else statement):
>
> $strings = array('str1', 'str2', 'str3', 'str4');
> $_th = in_array($myswitch, $strings)
> ? "user/$url/_thumb/$file"
> : "$gfx/file.gif";
>
> Micha
severin [ So, 24 Juni 2007 22:07 ] [ ID #1747373 ]

Re: switch easy

Message-ID: <467ecee0$0$10863$426a74cc [at] news.free.fr> from Séverin
Richard contained the following:

>Thank you for your answer
>
>I have many different extention to deal with and i will use switch
>rather than ternary operator.

I still find little use for switch and prefer to use an array instead.

--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011
Geoff Berrow [ Mo, 25 Juni 2007 01:59 ] [ ID #1747950 ]

Re: switch easy

..oO(Geoff Berrow)

>Message-ID: <467ecee0$0$10863$426a74cc [at] news.free.fr> from Séverin
>Richard contained the following:
>
>>Thank you for your answer
>>
>>I have many different extention to deal with and i will use switch
>>rather than ternary operator.
>
>I still find little use for switch and prefer to use an array instead.

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.

Micha
Michael Fesser [ Mo, 25 Juni 2007 02:30 ] [ ID #1747951 ]

Re: switch easy

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


JW
Janwillem Borleffs [ Mo, 25 Juni 2007 13:36 ] [ ID #1747955 ]

Re: switch easy

..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
Michael Fesser [ Mo, 25 Juni 2007 15:17 ] [ ID #1747956 ]

Re: switch easy

Geoff Berrow wrote:
> Message-ID: <467ecee0$0$10863$426a74cc [at] news.free.fr> from Séverin
> Richard contained the following:
>
>> Thank you for your answer
>>
>> I have many different extention to deal with and i will use switch
>> rather than ternary operator.
>
> I still find little use for switch and prefer to use an array instead.
>

IMHO the array has the following disadvantage over a switch, you don't get a
default value, you have to duplicate same value if more than one option will
generate the same result. A lot more work if you want a dynamic reault that
depends on many variables.

--

//Aho
Shion [ Di, 26 Juni 2007 06:37 ] [ ID #1748838 ]
PHP » alt.php » switch easy

Vorheriges Thema: how to not write password in code for using to mysql?
Nächstes Thema: Help with PHP, GraphViz and multidemnsional arrays (oh and nestedset data) :)