Connecting Tech Pros Worldwide Forums | Help | Site Map

Switch isset and $_get

wouter
Guest
 
Posts: n/a
#1: Oct 22 '06
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??

PleegWat
Guest
 
Posts: n/a
#2: Oct 22 '06

re: Switch isset and $_get


In article <453b9820$0$9457$ba620dc5@text.nova.planet.nl>, wouter
says...
Quote:
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
Ron
Guest
 
Posts: n/a
#3: Oct 22 '06

re: Switch isset and $_get


"wouter" <no@spam.nlwrote in message
news:453b9820$0$9457$ba620dc5@text.nova.planet.nl. ..
Quote:
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


wouter
Guest
 
Posts: n/a
#4: Oct 22 '06

re: Switch isset and $_get


OK if /else(if) it is...

Tx guys
Pedro Graca
Guest
 
Posts: n/a
#5: Oct 22 '06

re: Switch isset and $_get


wouter wrote:
Quote:
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?
Quote:
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.
Juliette
Guest
 
Posts: n/a
#6: Oct 22 '06

re: Switch isset and $_get


wouter wrote:
Quote:
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.:
Quote:
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
Pedro Graca
Guest
 
Posts: n/a
#7: Oct 22 '06

re: Switch isset and $_get


Juliette wrote:
Quote:
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.
Quote:
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.
Quote:
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*.
Juliette
Guest
 
Posts: n/a
#8: Oct 22 '06

re: Switch isset and $_get


Pedro Graca wrote:
Quote:
Juliette wrote:
Quote:
>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.
Quote:
>
Quote:
>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.
Quote:
>
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.

Quote:
>
Quote:
>In other words choosing the order of your cases is very important if you
>use this syntax when several cases can be true.
>
>
Pedro Graca
Guest
 
Posts: n/a
#9: Oct 23 '06

re: Switch isset and $_get


Juliette wrote:
Quote:
Pedro Graca wrote:
Quote:
>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*.
Jerry Stuckle
Guest
 
Posts: n/a
#10: Oct 23 '06

re: Switch isset and $_get


Pedro Graca wrote:
Quote:
Juliette wrote:
>
Quote:
>>Pedro Graca wrote:
>>
Quote:
>>>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.
jstucklex@attglobal.net
==================
Closed Thread