By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,747 Members | 2,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,747 IT Pros & Developers. It's quick & easy.

Problem with 'Switch'

P: n/a
I have using a 'switch' to retrieve the name of the website section through
sending a 'section' integer...

function title($section)
{
switch ($section)
{
case 0:
$output = "Introduction";
case 1:
$output = "login";
case 2:
$output = "choose_date";
case 3:
$output = "am_pm";
case 4:
$output = "hour";
case 5:
$output = "choose_time";
case 6:
$output = "slots";
case 7:
$output = "add_notes";
case 8:
$output = "confirm";
case 9:
$output = "booked";
default:
$output = "Introduction";
}
return $output;
}

However, no matter what number I sent to the command I am getting the
default case out (Introduction).

This seems like a very simple command, where am I going wrong???

Many thanks

Jamie
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Jamie Wright wrote:
I have using a 'switch' to retrieve the name of the website section through
sending a 'section' integer...

function title($section)
{
switch ($section)
{
case 0:
$output = "Introduction";
case 1:
$output = "login"; (snip) default:
$output = "Introduction";
}
return $output;
}

However, no matter what number I sent to the command I am getting the
default case out (Introduction).

This seems like a very simple command, where am I going wrong???


switch's cases fall through to next case, so that you can do

switch($a) {
case 0:
case 1:
whatever();
break;
case 2:
case 3:
somethingelse();
break;
default:
nomatch();
break;
}

Here, whatever() will be called when $a is 0 or 1; somethingelse() will
be called when $a is 2 or 3; and nomatch() will be called at all other
times.

In your script, when $section is 0, it will set $output to
"Introduction", then to "login", then ..., and finally to "Introduction"
again.
You need break statements in each case to stop the script from going on
to next case statements.
--
USENET would be a better place if everybody read: | to email me: use |
http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
http://www.netmeister.org/news/learn2quote2.html | header, textonly |
http://www.expita.com/nomime.html | no attachments. |
Jul 17 '05 #2

P: n/a

"Pedro Graca" <he****@hotpop.com> wrote in message
news:sl*******************@ID-203069.user.uni-berlin.de...
Jamie Wright wrote:
I have using a 'switch' to retrieve the name of the website section through sending a 'section' integer...

function title($section)
{
switch ($section)
{
case 0:
$output = "Introduction";
case 1:
$output = "login";

(snip)
default:
$output = "Introduction";
}
return $output;
}

However, no matter what number I sent to the command I am getting the
default case out (Introduction).

This seems like a very simple command, where am I going wrong???


switch's cases fall through to next case, so that you can do

switch($a) {
case 0:
case 1:
whatever();
break;
case 2:
case 3:
somethingelse();
break;
default:
nomatch();
break;
}

Here, whatever() will be called when $a is 0 or 1; somethingelse() will
be called when $a is 2 or 3; and nomatch() will be called at all other
times.

In your script, when $section is 0, it will set $output to
"Introduction", then to "login", then ..., and finally to "Introduction"
again.
You need break statements in each case to stop the script from going on
to next case statements.
--
USENET would be a better place if everybody read: | to email me: use |
http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
http://www.netmeister.org/news/learn2quote2.html | header, textonly |
http://www.expita.com/nomime.html | no attachments. |


Brilliant. Thanks

Jamie
Jul 17 '05 #3

P: n/a
To paraphrase Detective Deckard, how can a page not know what it is?

"Jamie Wright" <an**@anon.com> wrote in message
news:cc**********@news8.svr.pol.co.uk...
I have using a 'switch' to retrieve the name of the website section through sending a 'section' integer...

function title($section)
{
switch ($section)
{
case 0:
$output = "Introduction";
case 1:
$output = "login";
case 2:
$output = "choose_date";
case 3:
$output = "am_pm";
case 4:
$output = "hour";
case 5:
$output = "choose_time";
case 6:
$output = "slots";
case 7:
$output = "add_notes";
case 8:
$output = "confirm";
case 9:
$output = "booked";
default:
$output = "Introduction";
}
return $output;
}

However, no matter what number I sent to the command I am getting the
default case out (Introduction).

This seems like a very simple command, where am I going wrong???

Many thanks

Jamie

Jul 17 '05 #4

P: n/a

"Jamie Wright" <an**@anon.com> wrote in message
news:cc**********@news8.svr.pol.co.uk...

"Pedro Graca" <he****@hotpop.com> wrote in message
news:sl*******************@ID-203069.user.uni-berlin.de...
Jamie Wright wrote:
I have using a 'switch' to retrieve the name of the website section through sending a 'section' integer...

function title($section)
{
switch ($section)
{
case 0:
$output = "Introduction";
case 1:
$output = "login";

(snip)
default:
$output = "Introduction";
}
return $output;
}

However, no matter what number I sent to the command I am getting the
default case out (Introduction).

This seems like a very simple command, where am I going wrong???


switch's cases fall through to next case, so that you can do

switch($a) {
case 0:
case 1:
whatever();
break;
case 2:
case 3:
somethingelse();
break;
default:
nomatch();
break;
}

Here, whatever() will be called when $a is 0 or 1; somethingelse() will
be called when $a is 2 or 3; and nomatch() will be called at all other
times.

In your script, when $section is 0, it will set $output to
"Introduction", then to "login", then ..., and finally to "Introduction"
again.
You need break statements in each case to stop the script from going on
to next case statements.
--
USENET would be a better place if everybody read: | to email me: use |
http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
http://www.netmeister.org/news/learn2quote2.html | header, textonly |
http://www.expita.com/nomime.html | no attachments. |


Brilliant. Thanks

Jamie

You already got the switch/break answer, but if all you want to do is assign
a string constant, you could also have used an array filled with the 10
strings you need and use the $a as the index.

$arr = array("Introduction","login","choose_date","am_pm" ,"hour"); // add
the other 5 as well of course...
$output = $arr [$a];

Less typing, and I think more elegant as well. I haven't tried which
solution is faster at runtime. My guess would be the array version too.

HTH
Pjotr
HTH
Pjotr
Jul 17 '05 #5

P: n/a
"Pjotr Wedersteers" <x3****@westerterp.com> wrote in message
news:40***********************@dreader10.news.xs4a ll.nl...
-snip-
$arr = array("Introduction","login","choose_date","am_pm" ,"hour"); // add
the other 5 as well of course...
$output = $arr [$a];

Less typing, and I think more elegant as well. I haven't tried which
solution is faster at runtime.


And vulnerable to attack

$a = 9999;
echo $arr[$a];

Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.