PDA

View Full Version : PHP noobery (problem with switch statement)


]|V|[agnus
07-27-2004, 08:56 PM
I want to break a page up into different sections. These sections will be brought up depending on the value of a variable named $section.


switch ($section) {
case "immediate":
include("_immediate.php");
break;
case "future":
include("_future.php");
break;
default:
echo '
... HTML ...
';
}


Now if one of my nav links has an href like "index.php?section=immediate", shouldn't that grab the _immediate.php include then? Well, I mean, apparently it shouldn't, since it's not, so what am I missing? I tried specifying the POST scope for $section, but that had no effect. Perhaps I did that wrong as well. Help?

missing-score
07-27-2004, 09:12 PM
maybe you should use $_GET['section']?

]|V|[agnus
07-27-2004, 09:16 PM
Duh! You're right about that, if I'm wanting to be specific about my scope. However, leaving it with the more generic $section, shouldn't that also work?

Regardless, using the PROPER scope does make it work, so thanks.

missing-score
07-27-2004, 09:21 PM
im not sure you completley understand what scope means.

The method you mentioned would have worked in older versions, with register globals on... Basically, post and get vars were given global variables so that they could be accessed without using the $_POST and $_GET... However $_POST and $_GET are still global, and therefore in the same scope...

An example of a different scope would be a variable inside a function and a variable outside... The variable outside the function can be accessed from within the function, but not vice versa... the scope of the variable in the function is limited to the function itself.

]|V|[agnus
07-27-2004, 09:27 PM
well, more accurately, i don't think i understand scope as it pertains to languages like PHP. i would assume some of the ideas of scope are the same between PHP and JavaScript, since i know JavaScript has the same issues with variable scope with regard to whether it's within' a function or not.

my knowledge of scope comes mostly from ColdFusion, which is what i learned before i really got much into anything like JS or PHP. in ColdFusion, for instance, most variables can be referenced simply with their name. exceptions that i know of would be session variables which require the "session." prefix. session being the scope then. a bread and butter assigned value could be referred to again simply by the name or by "variables.name". values submitted via a form could be referred to by the name simply or "form.name".

maybe you already knew that, but i'm just trying to explain my idea of scope...

firepages
07-28-2004, 03:10 AM
its not really an issue of scope in this case , the issue is that once upon a time your GET $section variable would have been automatically registered by PHP , rather than having to grab it from the (now depracated) HTTP_GET_VARS array.

There was a debate about whether this was safe as indeed it made it easier for peeps to write insecure code, so the plan was hatched to stop auto-registration of GET,POST,COOKIE etc vars as a configuration default (though on many hosts your code will work as-is)

so the superglobals came along (_GET,_POST,_COOKIE,_SESSION,[& _REQUEST])

& thats where scope comes into play as the superglobals are indeed 'superglobal' in that $_GET['section'] will be available in all your functions and classes without being declared global

But in your case , unless register_globals is turned on in the php.ini (should be off by default) your $section variable does not even exist let alone have any scope.

]|V|[agnus
07-28-2004, 05:38 AM
great info, thanks!