This is a discussion on switch case - to require the break statements seems strange to me within the PHP General forums, part of the PHP Programming Forums category; Not that it is an issue, but just to understand the logic- Why do we have to use 'break' statements ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Not that it is an issue, but just to understand the logic-
Why do we have to use 'break' statements in each case? switch ($i) { case 0: echo "i equals 0"; break; case 1: echo "i equals 1"; break; case 2: echo "i equals 2"; break; } all 3 cases fire, even though $i only equals ONE of those case values (if I said that right). I mean if $i==1, then in other languages I don't expect the first or last case to fire! (?) Is the purpose just so one has the OPTION of letting them all fire, and turning that off with 'break'? Or is there a better reason? -G |
|
|||
|
On Aug 31, 8:46*pm, govi...@blisscode.com (Govinda) wrote:
> Not that it is an issue, but just to understand the logic- > Why do we have to use 'break' statements in each case? > > switch ($i) { > case 0: > * * *echo "i equals 0"; > * * *break; > case 1: > * * *echo "i equals 1"; > * * *break; > case 2: > * * *echo "i equals 2"; > * * *break; > > } > > all 3 cases fire, even though $i only equals ONE of those case values * > (if I said that right). > I mean if $i==1, then in other languages I don't expect the first or * > last case to fire! *(?) > Is the purpose just so one has the OPTION of letting them all fire, * > and turning that off with 'break'? > Or is there a better reason? > > -G Yes, the reason that break is required is to allow execution fall- through, such as wanting the same code to execute if $i is 0 or 1. Most, if not all, other C-style languages work this way. |
|
|||
|
Govinda a écrit :
> Or is there a better reason? What is exactly in $i ? A scalar integer, a string containing an integer ? A boolean ? What version of PHP ? -- Mickaël Wolff aka Lupus Michaelis http://lupusmic.org |
|
|||
|
Hi,
Well, I see a good reason anyway... U can have a lot of entry points and only one to exit... Like: switch ($i) { case 0: echo "\$i < 1"; case 1: echo "\$i < 2"; case 2: echo "\$i < 3"; default: echo "\$i > 2"; } Or switch ($i) { case 0: case 1: case 2: echo "\$i < 3"; break; default: echo "\$i > 2"; } Nothing says that you can only use it as an option one statement... On Mon, Sep 1, 2008 at 2:04 AM, Lupus Michaelis <mickael+php@lupusmic.org<mickael%2Bphp@lupusmic.o rg> > wrote: > Govinda a écrit : > >> Or is there a better reason? >> > > What is exactly in $i ? A scalar integer, a string containing an integer? > A boolean ? What version of PHP ? > > -- > Mickaël Wolff aka Lupus Michaelis > http://lupusmic.org > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- Thanks for your attention, Diogo Neves Web Developer @ SAPO.pt by PrimeIT.pt |
|
|||
|
Govinda schreef:
> Not that it is an issue, but just to understand the logic- > Why do we have to use 'break' statements in each case? > > switch ($i) { > case 0: > echo "i equals 0"; > break; > case 1: > echo "i equals 1"; > break; > case 2: > echo "i equals 2"; > break; > } > > all 3 cases fire, even though $i only equals ONE of those case values > (if I said that right). > I mean if $i==1, then in other languages I don't expect the first or > last case to fire! (?) > Is the purpose just so one has the OPTION of letting them all fire, and > turning that off with 'break'? pretty much, all code will be run inside the switch after the first case found to equate (match) until a break is hit. try it: foreach (range(0,2) as $i) { echo "\$i = $i -- running switch ... \n"; switch ($i) { case 0: echo "first case\n"; case 1: echo "second case\n"; case 2: echo "third case\n"; } } > Or is there a better reason? > > -G > |
|
|||
|
Diogo Neves a écrit :
> Hi, > > Well, I see a good reason anyway... > U can have a lot of entry points and only one to exit... > Like: I misunderstood the question :-/ I read Govinda had a strange behavior that ignore the break :-D -- Mickaël Wolff aka Lupus Michaelis http://lupusmic.org |
|
|||
|
At 6:46 PM -0600 8/31/08, Govinda wrote:
>Not that it is an issue, but just to understand the logic- >Why do we have to use 'break' statements in each case? > >switch ($i) { >case 0: > echo "i equals 0"; > break; >case 1: > echo "i equals 1"; > break; >case 2: > echo "i equals 2"; > break; >} > >all 3 cases fire, even though $i only equals ONE of those case >values (if I said that right). >I mean if $i==1, then in other languages I don't expect the first or >last case to fire! (?) >Is the purpose just so one has the OPTION of letting them all fire, >and turning that off with 'break'? >Or is there a better reason? > >-G The "break" is to separate each case (i.e., condition) The switch ($i) isn't even needed if you do it like this: switch (true) { case $i==0: echo "i equals 0"; break; case $i==1: echo "i equals 1"; break; case $i==2: echo "i equals 2"; break; } If you wanted to combine conditions, you could do this: switch (1) { case $i==-2: case $i==-1: case $i==0: echo "i is less than 0 but greater than -3 and is a counting number (i.e., no fraction)"; break; case $i==1: echo "i equals 1"; break; case $i==2: echo "i equals 2"; break; } Typed without checking and after my vacation. Cheers, tedd -- ------- http://sperling.com http://ancientstones.com http://earthstones.com |
|
|||
|
tedd schreef:
> At 6:46 PM -0600 8/31/08, Govinda wrote: >> Not that it is an issue, but just to understand the logic- >> Why do we have to use 'break' statements in each case? >> >> switch ($i) { >> case 0: >> echo "i equals 0"; >> break; >> case 1: >> echo "i equals 1"; >> break; >> case 2: >> echo "i equals 2"; >> break; >> } >> >> all 3 cases fire, even though $i only equals ONE of those case values >> (if I said that right). >> I mean if $i==1, then in other languages I don't expect the first or >> last case to fire! (?) >> Is the purpose just so one has the OPTION of letting them all fire, >> and turning that off with 'break'? >> Or is there a better reason? >> >> -G > > > The "break" is to separate each case (i.e., condition) > > The switch ($i) isn't even needed if you do it like this: > > switch (true) > { > case $i==0: > echo "i equals 0"; > break; > > case $i==1: > echo "i equals 1"; > break; > > case $i==2: > echo "i equals 2"; > break; > } this is 'true' ;-) and works very well when you want to check disparate truths but there are caveats: 1. it's less performant IIRC 2. there is no type checking, so auto-casting occurs during the test of each case's expression 3. it will become even less performant ... someone clever sod has a patch that heavily optimizes 'simple' switch statements ... see the internal mailing list archives for details (I can't remember the details) ... I gather this patch will eventually make it into the core, if it hasn't already. > > If you wanted to combine conditions, you could do this: > > switch (1) > { > case $i==-2: > case $i==-1: > case $i==0: > > echo "i is less than 0 but greater than -3 and is a counting number > (i.e., no fraction)"; > break; > > case $i==1: > echo "i equals 1"; > break; > > case $i==2: > echo "i equals 2"; > break; > } > > > Typed without checking and after my vacation. > > Cheers, > > tedd > |
|
|||
|
I wonder if this is a shared trait between C and PHP (since I understand PHP
is written in C) that the break; and the default: are placed for good practice in all switch statements since they prevent memory leaks? 2008/9/10 Jochem Maas <jochem@iamjochem.com> > tedd schreef: > > At 6:46 PM -0600 8/31/08, Govinda wrote: >> >>> Not that it is an issue, but just to understand the logic- >>> Why do we have to use 'break' statements in each case? >>> >>> switch ($i) { >>> case 0: >>> echo "i equals 0"; >>> break; >>> case 1: >>> echo "i equals 1"; >>> break; >>> case 2: >>> echo "i equals 2"; >>> break; >>> } >>> >>> all 3 cases fire, even though $i only equals ONE of those case values (if >>> I said that right). >>> I mean if $i==1, then in other languages I don't expect the first or last >>> case to fire! (?) >>> Is the purpose just so one has the OPTION of letting them all fire, and >>> turning that off with 'break'? >>> Or is there a better reason? >>> >>> -G >>> >> >> >> The "break" is to separate each case (i.e., condition) >> >> The switch ($i) isn't even needed if you do it like this: >> >> switch (true) >> { >> case $i==0: >> echo "i equals 0"; >> break; >> >> case $i==1: >> echo "i equals 1"; >> break; >> >> case $i==2: >> echo "i equals 2"; >> break; >> } >> > > this is 'true' ;-) and works very well when you want to > check disparate truths but there are caveats: > > 1. it's less performant IIRC > 2. there is no type checking, so auto-casting occurs during the > test of each case's expression > 3. it will become even less performant ... someone clever sod has > a patch that heavily optimizes 'simple' switch statements ... see > the internal mailing list archives for details (I can't remember the > details) ... I gather this patch will eventually make it into the core, > if it hasn't already. > > >> If you wanted to combine conditions, you could do this: >> >> switch (1) >> { >> case $i==-2: >> case $i==-1: >> case $i==0: >> >> echo "i is less than 0 but greater than -3 and is a counting number >> (i.e., no fraction)"; >> break; >> >> case $i==1: >> echo "i equals 1"; >> break; >> >> case $i==2: >> echo "i equals 2"; >> break; >> } >> >> >> Typed without checking and after my vacation. >> >> Cheers, >> >> tedd >> >> > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- Luke Slater |
|
|||
|
Luke schreef:
> I wonder if this is a shared trait between C and PHP (since I understand PHP > is written in C) that the break; and the default: are placed for good > practice in all switch statements since they prevent memory leaks? default is not required, never heard it was good practice to always put it in. I assume that the fact that php is written in C has no baring on the functionality of php's switch implementation. *in theory* one should not have to be concerned with mem leaks when using php, that kind of low level stuff is the exact reason C isn't generally used to build web apps. in practice mem leaks can be an issue, but this is generally in long running cli scripts ... and I don't think breaks left out of switch statements have ever been shown to be the cause ... that said leaving out a break from a switch case may completely change the logic of the code, but that's a seperate issue (see tedd's explaination) > 2008/9/10 Jochem Maas <jochem@iamjochem.com> > >> tedd schreef: >> >> At 6:46 PM -0600 8/31/08, Govinda wrote: >>>> Not that it is an issue, but just to understand the logic- >>>> Why do we have to use 'break' statements in each case? >>>> >>>> switch ($i) { >>>> case 0: >>>> echo "i equals 0"; >>>> break; >>>> case 1: >>>> echo "i equals 1"; >>>> break; >>>> case 2: >>>> echo "i equals 2"; >>>> break; >>>> } >>>> >>>> all 3 cases fire, even though $i only equals ONE of those case values (if >>>> I said that right). >>>> I mean if $i==1, then in other languages I don't expect the first or last >>>> case to fire! (?) >>>> Is the purpose just so one has the OPTION of letting them all fire, and >>>> turning that off with 'break'? >>>> Or is there a better reason? >>>> >>>> -G >>>> >>> >>> The "break" is to separate each case (i.e., condition) >>> >>> The switch ($i) isn't even needed if you do it like this: >>> >>> switch (true) >>> { >>> case $i==0: >>> echo "i equals 0"; >>> break; >>> >>> case $i==1: >>> echo "i equals 1"; >>> break; >>> >>> case $i==2: >>> echo "i equals 2"; >>> break; >>> } >>> >> this is 'true' ;-) and works very well when you want to >> check disparate truths but there are caveats: >> >> 1. it's less performant IIRC >> 2. there is no type checking, so auto-casting occurs during the >> test of each case's expression >> 3. it will become even less performant ... someone clever sod has >> a patch that heavily optimizes 'simple' switch statements ... see >> the internal mailing list archives for details (I can't remember the >> details) ... I gather this patch will eventually make it into the core, >> if it hasn't already. >> >> >>> If you wanted to combine conditions, you could do this: >>> >>> switch (1) >>> { >>> case $i==-2: >>> case $i==-1: >>> case $i==0: >>> >>> echo "i is less than 0 but greater than -3 and is a counting number >>> (i.e., no fraction)"; >>> break; >>> >>> case $i==1: >>> echo "i equals 1"; >>> break; >>> >>> case $i==2: >>> echo "i equals 2"; >>> break; >>> } >>> >>> >>> Typed without checking and after my vacation. >>> >>> Cheers, >>> >>> tedd >>> >>> >> -- >> PHP General Mailing List (http://www.php.net/) >> To unsubscribe, visit: http://www.php.net/unsub.php >> >> > > |