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

Switch isset and $_get

P: n/a
hey hi.....

I wanna make a switch wich does this:

if pagid is set do A,
if catid is set do B,
if projectid is set do C,
else do D.

So i was thinking something like this:

switch()
{
case isset ($_GET['pagid']):
{
echo "pagid= set";
}
break;
case isset ($_GET['catid']):
{
echo "catid= set";
}
break;
case isset ($_GET['projectid']):
{
echo "projectid= set";
}
break;
default:
{
echo "nothing set";
}
}
wich works great in my mind but not on my server....

Anybody here know how to do this in a way wich does work??
Oct 22 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
In article <45**********************@text.nova.planet.nl>, wouter
says...
hey hi.....

I wanna make a switch wich does this:

if pagid is set do A,
if catid is set do B,
if projectid is set do C,
else do D.

So i was thinking something like this:

switch()
{
case isset ($_GET['pagid']):
{
echo "pagid= set";
}
break;
case isset ($_GET['catid']):
{
echo "catid= set";
}
break;
case isset ($_GET['projectid']):
{
echo "projectid= set";
}
break;
default:
{
echo "nothing set";
}
}
wich works great in my mind but not on my server....

Anybody here know how to do this in a way wich does work??
You don't need a switch here, but a if...elseif...else structure:

if( isset($_GET['pagid']) )
{
echo "pagid= set";
}
elseif( isset($_GET['catid']) )
{
echo "catid= set";
}
elseif( isset($_GET['projectid']) )
{
echo "projectid= set";
}
else
{
echo "nothing set";
}

--
PleegWat
Remove caps to reply
Oct 22 '06 #2

P: n/a
Ron
"wouter" <no@spam.nlwrote in message
news:45**********************@text.nova.planet.nl. ..
hey hi.....

I wanna make a switch wich does this:

if pagid is set do A,
if catid is set do B,
if projectid is set do C,
else do D.

So i was thinking something like this:

switch()
{
case isset ($_GET['pagid']): {
echo "pagid= set";
}
break;
case isset ($_GET['catid']):
{
echo "catid= set";
}
break;
case isset ($_GET['projectid']):
{
echo "projectid= set";
}
break;
default:
{
echo "nothing set";
}
}
wich works great in my mind but not on my server....

Anybody here know how to do this in a way wich does work??
You are mis-using the switch syntax check the manual
Switch only tests for different values in a single variable.

I assume from your text that the states are either mutually exclusive or
that pagid being set has precedence over the other variables
you could simply do this as if / elsif /elsif /else.

Cheers

Ron
Oct 22 '06 #3

P: n/a
OK if /else(if) it is...

Tx guys
Oct 22 '06 #4

P: n/a
wouter wrote:
hey hi.....

I wanna make a switch wich does this:

if pagid is set do A,
if catid is set do B,
if projectid is set do C,
else do D.

So i was thinking something like this:

switch()
{
case isset ($_GET['pagid']):
{
echo "pagid= set";
}
break;
case isset ($_GET['catid']):
{
echo "catid= set";
}
break;
case isset ($_GET['projectid']):
{
echo "projectid= set";
}
break;
default:
{
echo "nothing set";
}
}
wich works great in my mind but not on my server....
You need something to switch on
switch ($SOMETHING)
^^^^^^^^^^

The case expressions *must* all be different. In your example, as
isset() returns either `true` or `false` at least two of them will
be equal.

PHP will then compare the $SOMETHING to the case expressions and
continue the script execution from the case expression that matches.
If there are two or more matches, how will PHP know from which match to
start?
Anybody here know how to do this in a way wich does work??
with if()s

if (isset($_GET['pagid'])) { echo "pagid= set"; }
elseif (isset($_GET['catid'])) { echo "catid= set"; }
elseif (isset($_GET['projectid'])) { echo "projectid= set"; }
else { echo "nothing set"; }

You might want to think about what happens if there's more than one of
those $_GET parameters set ...

Happy Coding :)

--
File not found: (R)esume, (R)etry, (R)erun, (R)eturn, (R)eboot

I don't check the dodgeit address (very often).
If you *really* need to mail me,
use the address in the Reply-To header.
Oct 22 '06 #5

P: n/a
wouter wrote:
hey hi.....

I wanna make a switch wich does this:

if pagid is set do A,
if catid is set do B,
if projectid is set do C,
else do D.

So i was thinking something like this:

switch()
{
case isset ($_GET['pagid']):
{
echo "pagid= set";
}
break;
case isset ($_GET['catid']):
{
echo "catid= set";
}
break;
case isset ($_GET['projectid']):
{
echo "projectid= set";
}
break;
default:
{
echo "nothing set";
}
}
wich works great in my mind but not on my server....

Anybody here know how to do this in a way wich does work??

switch( true )
{

Contrary to the (non-)problem highlighted by another poster, having
several expressions here that could be true, is not a problem.

Switch will execute the first case it comes across which is valid. If
that case is closed by 'break;', it will then terminate the switch, if
the case is closed by 'continue;' it will continue to evaluate the other
options too.
In other words choosing the order of your cases is very important if you
use this syntax when several cases can be true.

On another note: the curly braces within your cases are not needed, a
brace surrounding your isset's is recommended. I.e.:
case isset ($_GET['pagid']):
{
echo "pagid= set";
}
break;
Would become:
case ( isset ($_GET['pagid']) ):
echo "pagid= set";
break;

or further on the case statement:
case ( isset ($_GET['pagid']) === true ):

Grz, Juliette
Oct 22 '06 #6

P: n/a
Juliette wrote:
Contrary to the (non-)problem highlighted by another poster, having
several expressions here that could be true, is not a problem.
I apologize for not checking before posting (I was thinking C, where it
is illegal to have different cases with the same value).

Thank you for catching my error and calling attention to it.
Switch will execute the first case it comes across which is valid. If
that case is closed by 'break;', it will then terminate the switch, if
the case is closed by 'continue;' it will continue to evaluate the other
options too.
`continue` will behave exactly like break.

When there is no match for the case expressions and there are several
`default` cases (illegal in C too), the one chosen seems to be the last
and not, as I expected, the first.
In other words choosing the order of your cases is very important if you
use this syntax when several cases can be true.

<?php
for ($i=0; $i<3; ++$i) {
switch ($i) {
case 1: echo "first case 1\n"; break;
case 2: echo "first case 2\n"; continue;
default: echo "first default\n"; continue;
case 1: echo "second case 1\n"; continue;
case 2: echo "second case 2\n"; break;
default: echo "second default\n"; break;
}
}
?>
--
I (almost) never check the dodgeit address.
If you *really* need to mail me, use the address in the Reply-To
header with a message in *plain* *text* *without* *attachments*.
Oct 22 '06 #7

P: n/a
Pedro Graca wrote:
Juliette wrote:
>Contrary to the (non-)problem highlighted by another poster, having
several expressions here that could be true, is not a problem.

I apologize for not checking before posting (I was thinking C, where it
is illegal to have different cases with the same value).

Thank you for catching my error and calling attention to it.
No probs, happens to the best of us.
>
>Switch will execute the first case it comes across which is valid. If
that case is closed by 'break;', it will then terminate the switch, if
the case is closed by 'continue;' it will continue to evaluate the other
options too.

`continue` will behave exactly like break.
Oops, I posted this bit too quickly - not having anything, i.e. not
break, nor continue, will let the switch 'continue' / fall through and
continue with the next case.
>
When there is no match for the case expressions and there are several
`default` cases (illegal in C too), the one chosen seems to be the last
and not, as I expected, the first.
I have never tested a switch with several 'default:' cases - as far as I
know, that /should/ be illegal in php too, but it being the flexible
language it is, it may only throw a warning or not even that.

Thanks for pointing out that it will use the last one in that case. I
would have expected it to execute both in the order it came across them,
but then again, thinking it over, the default in php by definition
should be the last case, so it only executing the last one shouldn't
surprise me.

No matter what, having several 'default:' cases is *always* highly
inadvisable.

>
>In other words choosing the order of your cases is very important if you
use this syntax when several cases can be true.

Oct 22 '06 #8

P: n/a
Juliette wrote:
Pedro Graca wrote:
>When there is no match for the case expressions and there are several
`default` cases (illegal in C too), the one chosen seems to be the last
and not, as I expected, the first.

No matter what, having several 'default:' cases is *always* highly
inadvisable.
SCNR
Having several `case CONSTANT:` with the same CONSTANT is ok? :)
<?php
$var = true;

echo "First set: ";
switch ($var) {
default: echo 'No variables in the list are'; break;
case isset($a): echo 'a is'; break;
case isset($b): echo 'b is'; break;
case isset($c): echo 'c is'; break;
case isset($d): echo 'd is'; break;
}
echo " set.\n";

$b = $c = 42;
echo "Second set: ";
switch ($var) {
default: echo 'No variables in the list are'; break;
case isset($a): echo 'a is'; break;
case isset($b): echo 'b is'; break;
case isset($c): echo 'c is'; break;
case isset($d): echo 'd is'; break;
}
echo " set.\n";
?>

To the Original Poster: don't use this code!

--
I (almost) never check the dodgeit address.
If you *really* need to mail me, use the address in the Reply-To
header with a message in *plain* *text* *without* *attachments*.
Oct 23 '06 #9

P: n/a
Pedro Graca wrote:
Juliette wrote:
>>Pedro Graca wrote:
>>>When there is no match for the case expressions and there are several
`default` cases (illegal in C too), the one chosen seems to be the last
and not, as I expected, the first.

No matter what, having several 'default:' cases is *always* highly
inadvisable.


SCNR
Having several `case CONSTANT:` with the same CONSTANT is ok? :)
<?php
$var = true;

echo "First set: ";
switch ($var) {
default: echo 'No variables in the list are'; break;
case isset($a): echo 'a is'; break;
case isset($b): echo 'b is'; break;
case isset($c): echo 'c is'; break;
case isset($d): echo 'd is'; break;
}
echo " set.\n";

$b = $c = 42;
echo "Second set: ";
switch ($var) {
default: echo 'No variables in the list are'; break;
case isset($a): echo 'a is'; break;
case isset($b): echo 'b is'; break;
case isset($c): echo 'c is'; break;
case isset($d): echo 'd is'; break;
}
echo " set.\n";
?>

To the Original Poster: don't use this code!
Well, it doesn't give an error, but it's operation is specified. So I
suspect if how it works changes you won't be able to do ant hying about it.

The bottom line is - the only defined behavior is to have a single
instance of a specific CONSTANT and a single default.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Oct 23 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.