473,652 Members | 3,059 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9706
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...e lse 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.nlwrot e in message
news:45******** **************@ text.nova.plane t.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*******@attgl obal.net
=============== ===
Oct 23 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
11869
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 the top of my script I assign a few variables to incoming GET and POST values. $login = clean($_POST, 30); $passwd = clean($_POST, 30);
4
2061
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, but with limited success. here are two versions of code: the 1st one is executing just 1st loaded data, ie: if case is "id_001" it will print "data for id #001", and will, in case there is nothing in url like "index.php?action=" print "nothing"...
8
3057
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', 'All Users'); $page->assign('tableheader', 'All Users'); $page->assign('sqlfile', 'sql_all.php'); break; case 'sqlprof':
25
2485
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 request www. my_site .com/chair.htm everything would be normal.
2
37157
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 not possible. But I tested with the "input type="button" and the clicking of the event is not happened. Can any one tell me why. The code: <html> <body>
2
2453
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 "\n</br>".getenv("QUERY_STRING");
8
2381
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 what try each successive one in turn as we can alternate between the two. But it doesn't seem to be working.
2
2550
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 != "") Is this something I need to do? Would I get "undefined" errors if I just did this instead?
2
3169
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> <label for="title">TITLE: </label> </td> <td class="input"> <input id="title" name="title" type="text" size="80" value="<?php if (!empty($title)) {echo $title;} ?>" />
0
8367
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8279
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8811
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8589
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7302
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5619
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4145
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1914
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.