473,382 Members | 1,717 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Switch isset and $_get

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
9 9670
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
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
OK if /else(if) it is...

Tx guys
Oct 22 '06 #4
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Dan | last post by:
I was trying to troubleshoot a login page that doesn't work - it keeps saying the login/password is missing - when my tracing discovered this peculiar behavior. register_globals is off, so at...
4
by: JaNE | last post by:
I'm trying to get some data from MySql database and to create switch based on those data, but as an php beginer, I have no clear idea how to do this. have tryed many options which loked promising,...
8
by: sigzero | last post by:
I have a url that ends with ?pid=sqlall. I am using the following to try and get at the "sqlall". I have read many sites to no avail. switch ($_GET) { case 'sqlall': $page->assign('pagetitle',...
25
by: wd | last post by:
I want my server to send a 404 header if a URL with a query string is requested. So if a browser or spider requests something like www. my_site .com?p=chair they would get a 404... But if they...
2
by: sathyashrayan | last post by:
Dear group, My question may be novice. I have seen codes where the isset() is used to test weather a user's session ($_SESSION) is set before entering a page. A kind of direct access to a page is...
2
by: Adam Baker | last post by:
I'm not sure whether the following is a problem with PHP or my Apache server. Minimal example: <?php if (isset($dummy)) echo "Set."; else echo "Not set."; echo...
8
by: Simon Dean | last post by:
Im taking Im doing something stupid here? I thought it was clever... just learned a little more about isset. $a = (isset($_GET)) ? $_GET : (isset($_POST)) ? $_POST : ""; I guess you can see...
2
by: Bill H | last post by:
I have gotten into the habit of using isset for every $_POST variable, checking that it is set before I even check to see if it contains anything. For example: if (isset($_POST) && $_POST != "")...
2
by: Annalyzer | last post by:
My form looks like this: <form action="handle_event.php" method="POST" enctype="multipart/form-data"> <table id="event_edit" border="0"> <tr> <td> ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.