Re: Re: alter switch variable inside case?
| From: | @ Edwin | Date: | Sun, 01 Sep 2002 12:39:41 +0000 |
| Subject: | Re: Re: alter switch variable inside case? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-114826@lists.php.net to get a copy of this message | ||
I'm not sure if you've already tried the idea I posted earlier. Anyway, I tried it myself and it works! (At least with this simple example...)
<!-- whatevername.php -->
<?php
echo 'myvar BEFORE the switch : ' . $_GET['myvar'];
echo '<hr />';
switch ($_GET['myvar']){
case 1: $_GET['myvar'] = 2;
case 2: echo "Hello!<br />"; break;
case 3: $_GET['myvar'] = 4;
case 4: echo "Hello again!<br />"; break;
}
echo '<hr />';
echo 'myvar AFTER the switch : ' . $_GET['myvar'];
?>
<!-- end of whatevername.php -->
Try it. For example,
http://www.your_domain_name.com/whatevername.php?myvar=1
will change myvar to 2 and will echo "Hello!" (without executing the other "cases"). Then,
http://www.your_domain_name.com/whatevername.php?myvar=3
will change myvar to 4 and will echo "Hello again!".
As you can see, the key is using "break" ONLY in places you want to "break"...
So, setting the variable in the first (or third) case CAN "cause execution of the second [or fourth] case segment".
- E
Could you elaborate? In the example I posted, setting foo=step1 in the first case segment does NOT cause execution of the second case segment, as would be the case if it was actually re-evaluating the match upon subsequent case statements. --- CHAILLAN Nicolas <nicos@php.net> wrote: Yes you can. -- Merci de nous avoir choisi. - Thanks you for your choice. Nicos - CHAILLAN Nicolas nicos@worldakt.com nicos@php.net www.GroupAKT.com - H饕ergement Group. www.WorldAKT.com - H饕ergement de sites Internet "Joe Janitor" <joe_janitor@yahoo.com> a 馗rit dans le message de news: 20020831181245.45286.qmail@web10803.mail.yahoo.com..._________________________________________________________________ ハイセンスな商品を気軽に購入 MSN ショッピング http://shopping.msn.co.jp/I'd like to be able to modify the switch variable inside a case statement, like this: switch ($foo) { case 'step2':-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php __________________________________________________ Do You Yahoo!? Yahoo! Finance - Get real-time stock quotes http://finance.yahoo.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.phpdo_step2(); if ($error) $foo='step1'; //repeat step1break; case 'step1':do_step1();break; case 'a_third_thing':do_something_else();break; } Can you modify the variable ($foo) inside a case statement and have it evaluated for subsequent 'case's like this? If not, I will have to revert to a series of if statements, in which this can be done: if ($foo=='step2') { do_step2(); if ($error) $foo='step1'; } if ($foo=='step1') { do_step1(); } etc. Switch-case seems cleaner, and I'd prefer to stick with it. __________________________________________________ Do You Yahoo!? Yahoo! Finance - Get real-time stock quotes http://finance.yahoo.com