<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:admin="http://webns.net/mvcb/"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
	<channel> 

	<title>Comments on: PHP - same trivial code behaving differently in two places</title>
	<link>http://ask.metafilter.com/39954/PHP-same-trivial-code-behaving-differently-in-two-places/</link>
	<description>Comments on Ask MetaFilter post PHP - same trivial code behaving differently in two places</description>
	<pubDate>Sun, 11 Jun 2006 12:51:27 -0800</pubDate>
	<lastBuildDate>Sun, 11 Jun 2006 12:51:27 -0800</lastBuildDate>
	<language>en-us</language>
	<docs>http://blogs.law.harvard.edu/tech/rss</docs>
	<ttl>60</ttl>

	<item>
		<title>Question: PHP - same trivial code behaving differently in two places</title>
		<link>http://ask.metafilter.com/39954/PHP-same-trivial-code-behaving-differently-in-two-places</link>	
		<description>Short piece of PHP code  - all it does is set a variable and POST it back to the same PHP code - that works when executed on my hosted web site, but doesn&apos;t work when executed on my local machine running XAMPP.


 &lt;br /&gt;&lt;br /&gt; All I&apos;m doing is setting a variable, and POSTing it to the same piece of PHP code, so that the first time the code executes (no variable set yet) one thing happens, and the second time the code executes (variable has now been set and POSTed) something different happens.&lt;br&gt;
&lt;br&gt;
My web host is running PHP 4 on Apache on Linux, and locally I&apos;m running PHP 5 on Apache on Windows (via XAMPP.)&lt;br&gt;
&lt;br&gt;
I&apos;d like to know how to fix the code so it works in both locations, if possible.&lt;br&gt;
&lt;br&gt;
On my desktop all it does is keep going to Case (0) where the variable $answer has not been set. On my web host it progresses through the three cases each time you click the button, so after the second click it hits the place where it echoes DONE and finishes.&lt;br&gt;
&lt;br&gt;
Warning: I&apos;m self-taught - the whole thing may be a bad way of doing things. I&apos;m willing to accept an alternate solution, but I&apos;d still like to know why this doesn&apos;t work in both environments:&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;head&gt;&lt;br&gt;
&lt;title&gt;Test of Post&lt;/title&gt;&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;/head&gt;&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Testing Post&lt;br&gt;
&lt;br&gt;&lt;br&gt;
&lt; ?php # if ($answer=1) { $switchflag=1; } if ($answer&gt; 1) {&lt;br&gt;
	$switchflag = 2;&lt;br&gt;
}&lt;br&gt;
if (!isset($answer)) {&lt;br&gt;
	$switchflag = 0;&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
switch ($switchflag) {&lt;br&gt;
	case (0):&lt;br&gt;
	# Case where answer =0 never been through yet&lt;br&gt;
echo &quot;&lt;br&gt;&lt;br&gt;First time through, because no value for answer has been passed.&lt;br&gt;&lt;br&gt;&quot;;&lt;br&gt;
echo &quot;&quot;;&lt;br&gt;
echo &quot;&quot;;&lt;br&gt;
echo &quot;&quot;;&lt;br&gt;
break;&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
switch ($switchflag) {&lt;br&gt;
	case (1):&lt;br&gt;
echo &quot;&lt;br&gt;&lt;br&gt;Second time through because answer has been passed with a value of 1.&quot;;&lt;br&gt;
echo &quot;&quot;;&lt;br&gt;
echo &quot;&quot;;&lt;br&gt;
echo &quot;&quot;;&lt;br&gt;
break;&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
switch ($switchflag) {&lt;br&gt;
	case (2):&lt;br&gt;
	# Last time through&lt;br&gt;
	echo &quot;&lt;br&gt;&lt;br&gt;&lt;br&gt;DONE&quot;;&lt;br&gt;
break;&lt;br&gt;
}&lt;br&gt;
?&amp;gt;&lt;br&gt;
&lt;br&gt;
&lt;/&gt;</description>
		<guid isPermaLink="false">post:ask.metafilter.com,2006:site.39954</guid>
		<pubDate>Sun, 11 Jun 2006 12:48:58 -0800</pubDate>
		<dc:creator>lockedroomguy</dc:creator>
		
			<category>PHP</category>
		
			<category>POST</category>
		
	</item> <item>
		<title>By: lockedroomguy</title>
		<link>http://ask.metafilter.com/39954/PHP-same-trivial-code-behaving-differently-in-two-places#616304</link>	
		<description>Whoops, code didn&apos;t post properly. Working on fixing asap. &lt;br&gt;
&lt;br&gt;
Sorry to waste your time.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.39954-616304</guid>
		<pubDate>Sun, 11 Jun 2006 12:51:27 -0800</pubDate>
		<dc:creator>lockedroomguy</dc:creator>
	</item><item>
		<title>By: lockedroomguy</title>
		<link>http://ask.metafilter.com/39954/PHP-same-trivial-code-behaving-differently-in-two-places#616308</link>	
		<description>I can&apos;t figure out how to post the code so it shows up.&lt;br&gt;
&lt;br&gt;
All I want to do is have a button. When the user clicks it, it sets a variable and calls the same piece of code again. But this time, it POSTS the variable like this:&lt;br&gt;
&lt;br&gt;
action=\&quot;testpost.php?answer=1\&quot; method=\&quot;post\&quot;&amp;gt;&quot;;&lt;br&gt;
&lt;br&gt;
At the beginning of the code I have a series of Switch and Case statements to test the value of $answer and then execute the appropriate piece of code based on that.&lt;br&gt;
&lt;br&gt;
You can see it actually working here:&lt;br&gt;
&lt;br&gt;
http://www.mysteryscenemag.com/phptest/testpost.php&lt;br&gt;
&lt;br&gt;
The exact same code just repeats the first page over and over on my local machine.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.39954-616308</guid>
		<pubDate>Sun, 11 Jun 2006 12:56:51 -0800</pubDate>
		<dc:creator>lockedroomguy</dc:creator>
	</item><item>
		<title>By: devilsbrigade</title>
		<link>http://ask.metafilter.com/39954/PHP-same-trivial-code-behaving-differently-in-two-places#616323</link>	
		<description>register_globals is off locally and on on the server, likely. use $_POST[myvar] to access POSTed variables</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.39954-616323</guid>
		<pubDate>Sun, 11 Jun 2006 13:13:46 -0800</pubDate>
		<dc:creator>devilsbrigade</dc:creator>
	</item><item>
		<title>By: devilsbrigade</title>
		<link>http://ask.metafilter.com/39954/PHP-same-trivial-code-behaving-differently-in-two-places#616327</link>	
		<description>actually, if all you&apos;re sending is answer=1 on the URL line, you should be using GET, and you should be using $_GET</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.39954-616327</guid>
		<pubDate>Sun, 11 Jun 2006 13:14:41 -0800</pubDate>
		<dc:creator>devilsbrigade</dc:creator>
	</item><item>
		<title>By: devilsbrigade</title>
		<link>http://ask.metafilter.com/39954/PHP-same-trivial-code-behaving-differently-in-two-places#616329</link>	
		<description>And, just because I don&apos;t read the qusetion all the way through, I&apos;d probably use a session variable instead of a URL flag (or use both). YMMV. Its an alright way of doing it.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.39954-616329</guid>
		<pubDate>Sun, 11 Jun 2006 13:15:57 -0800</pubDate>
		<dc:creator>devilsbrigade</dc:creator>
	</item><item>
		<title>By: cerbous</title>
		<link>http://ask.metafilter.com/39954/PHP-same-trivial-code-behaving-differently-in-two-places#616332</link>	
		<description>devilsbrigade has it. See &lt;a href=&quot;http://de3.php.net/register_globals&quot;&gt;this&lt;/a&gt; and &lt;a href=&quot;http://de3.php.net/manual/en/reserved.variables.php&quot;&gt;this&lt;/a&gt; for more info.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.39954-616332</guid>
		<pubDate>Sun, 11 Jun 2006 13:21:28 -0800</pubDate>
		<dc:creator>cerbous</dc:creator>
	</item><item>
		<title>By: lockedroomguy</title>
		<link>http://ask.metafilter.com/39954/PHP-same-trivial-code-behaving-differently-in-two-places#616339</link>	
		<description>Thanks - it looks like I&apos;ve got my answer from devil&apos;s brigade! But it will take me a while to understand these and understand exactly what I have to do - I&apos;ll mark best answers later tonight. &lt;br&gt;
&lt;br&gt;
Anyone know of a nice layman&apos;s explanation of the difference between GET, POST, $_GET, and session variables anywhere?</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.39954-616339</guid>
		<pubDate>Sun, 11 Jun 2006 13:30:11 -0800</pubDate>
		<dc:creator>lockedroomguy</dc:creator>
	</item><item>
		<title>By: reklaw</title>
		<link>http://ask.metafilter.com/39954/PHP-same-trivial-code-behaving-differently-in-two-places#616348</link>	
		<description>Layman&apos;s explanation of GET/POST:&lt;br&gt;
&lt;br&gt;
GET = variables contained in a link, like www.example.com?aaa=1&amp;amp;bbb=2&lt;br&gt;
POST = variables from a form submitted with the submit button with method=post&lt;br&gt;
&lt;br&gt;
For most purposes that&apos;s all you need to know.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.39954-616348</guid>
		<pubDate>Sun, 11 Jun 2006 13:44:01 -0800</pubDate>
		<dc:creator>reklaw</dc:creator>
	</item><item>
		<title>By: Khalad</title>
		<link>http://ask.metafilter.com/39954/PHP-same-trivial-code-behaving-differently-in-two-places#616354</link>	
		<description>Using &lt;code&gt;$_GET&lt;/code&gt; and &lt;code&gt;$_POST&lt;/code&gt; is preferred to relying on &lt;i&gt;register_globals&lt;/i&gt;. So you should definitely switch to using those. Specifically, replace all occurences of &lt;code&gt;$answer&lt;/code&gt; with &lt;code&gt;$_GET[&apos;answer&apos;]&lt;/code&gt;.&lt;br&gt;
&lt;br&gt;
By the way, you made a typo/error on the very first line. &apos;&lt;code&gt;=&lt;/code&gt;&apos; is assignment and &apos;&lt;code&gt;==&lt;/code&gt;&apos; is comparison. Change that to&lt;br&gt;
&lt;br&gt;
&lt;code&gt;if ($_GET[&apos;answer&apos;]&lt;b&gt;==&lt;/b&gt;1).&lt;/code&gt;</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.39954-616354</guid>
		<pubDate>Sun, 11 Jun 2006 14:00:34 -0800</pubDate>
		<dc:creator>Khalad</dc:creator>
	</item><item>
		<title>By: jellicle</title>
		<link>http://ask.metafilter.com/39954/PHP-same-trivial-code-behaving-differently-in-two-places#616372</link>	
		<description>You do need to have a good understanding of HTTP variables before you can effectively program a web application.&lt;br&gt;
&lt;br&gt;
When you submit a form, the form fields end up as POST variables.  When you get a URL that looks like &quot;?foo=bar&quot;, foo ends up as a GET variable.&lt;br&gt;
&lt;br&gt;
Now, PHP gets these, but doesn&apos;t automatically make them available as PHP variables that you can perform program logic upon.  If the PHP configuration setting register_globals is on, PHP will make them available automatically.  This is a major security risk.  register_globals should not be on.  In this, more common case, the variables are still available to PHP, but only in the special arrays _GET and _POST.  This forces you to THINK about where the data is coming from when you write your program, which is a good thing.&lt;br&gt;
&lt;br&gt;
Your form is confusing the idea of GET and POST variables.  When you change the URL of the form&apos;s ACTION attribute, you&apos;re making a GET variable.  If you were to actually change the form fields, you&apos;d be changing the POST variables.&lt;br&gt;
&lt;br&gt;
I know you&apos;re just learning now.  But it&apos;s important to know what&apos;s going on behind the scenes, otherwise when you go to write a real program, you *will* screw it up for lack of that knowledge.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.39954-616372</guid>
		<pubDate>Sun, 11 Jun 2006 14:19:50 -0800</pubDate>
		<dc:creator>jellicle</dc:creator>
	</item><item>
		<title>By: lockedroomguy</title>
		<link>http://ask.metafilter.com/39954/PHP-same-trivial-code-behaving-differently-in-two-places#616437</link>	
		<description>Whoa.&lt;br&gt;
&lt;br&gt;
So even if I say Method=&quot;post&quot; then I can be doing a GET? &lt;br&gt;
&lt;br&gt;
This seems to reflect another problem I&apos;ve never understood. If GET is supposed to show the variable value in the url, and POST doesn&apos;t, then why does the variable value show in my urls which use POST.&lt;br&gt;
&lt;br&gt;
I&apos;m guessing jellicle&apos;s answer is skinning up against my misconception. There&apos;s POST, and then there are POST variables, and there&apos;s GET, and then there are GET variables? You can use GET and make a POST variable, and use POST and make a GET variable? I had thought that if you used Method=post then you got a POST variable, end of story. Where can I look to understand this better?&lt;br&gt;
&lt;br&gt;
This is why I was asking if there was a good novice description somewhere. I&apos;ve looked at the PHP manual online, including all the added comments, and I&apos;ve got a PHP manual, but I am still having a problem with what-the-heck GET, POST, Get variables, POST variables, _GET, session variables, etc are. and when to use which.&lt;br&gt;
&lt;br&gt;
Book, course, or online references welcome.&lt;br&gt;
&lt;br&gt;
Thanks to all responders so far. You&apos;ve helped.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.39954-616437</guid>
		<pubDate>Sun, 11 Jun 2006 16:43:27 -0800</pubDate>
		<dc:creator>lockedroomguy</dc:creator>
	</item><item>
		<title>By: AmbroseChapel</title>
		<link>http://ask.metafilter.com/39954/PHP-same-trivial-code-behaving-differently-in-two-places#616438</link>	
		<description>&lt;strong&gt;&lt;em&gt;When you submit a form, the form fields end up as POST variables. &lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;br&gt;
... unless your form&apos;s action is set to &quot;get&quot;!&lt;br&gt;
&lt;strong&gt;&lt;em&gt;&lt;br&gt;
Your form is confusing the idea of GET and POST variables.&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;br&gt;
No it&apos;s not, it&apos;s just using both, which as far as I know is perfectly legal.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.39954-616438</guid>
		<pubDate>Sun, 11 Jun 2006 16:44:43 -0800</pubDate>
		<dc:creator>AmbroseChapel</dc:creator>
	</item><item>
		<title>By: AmbroseChapel</title>
		<link>http://ask.metafilter.com/39954/PHP-same-trivial-code-behaving-differently-in-two-places#616439</link>	
		<description>The key difference between &lt;strong&gt;get&lt;/strong&gt; and &lt;strong&gt;post&lt;/strong&gt; is that &lt;strong&gt;post&lt;/strong&gt; should be used for anything which &lt;em&gt;changes something on the server&lt;/em&gt;.&lt;br&gt;
&lt;br&gt;
If you&apos;re seaching google, it uses get, because it&apos;s literally &lt;em&gt;getting&lt;/em&gt; something from google&apos;s database: it asks a question of the database and you get your answer back.&lt;br&gt;
&lt;br&gt;
If you&apos;re buying something online, on the other hand, that should use post, because it&apos;s deducting something from your bank account and adding something to their database of &quot;stuff to send out to customers&quot;. There are before and after states, and in the &quot;after&quot; state, you&apos;re down $50 and they&apos;re up $50.&lt;br&gt;
&lt;br&gt;
So that&apos;s why, if you reload a page created by a post, the browser should give you an &quot;are you sure you want to do this?&quot; error message.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.39954-616439</guid>
		<pubDate>Sun, 11 Jun 2006 16:50:33 -0800</pubDate>
		<dc:creator>AmbroseChapel</dc:creator>
	</item><item>
		<title>By: AmbroseChapel</title>
		<link>http://ask.metafilter.com/39954/PHP-same-trivial-code-behaving-differently-in-two-places#616440</link>	
		<description>&lt;strong&gt;&lt;em&gt;I had thought that if you used Method=post then you got a POST variable, end of story.&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;br&gt;
That&apos;s right. But your form uses both, if it&apos;s got its action set to &quot;scriptname.php?foo=bar&quot; that&apos;s a hard-coded GET, and if the method is set to POST for the rest of the form then you&apos;re using both. I&apos;m not sure why it&apos;s coded that way but obviously it works. Perhaps it&apos;s just to make the form simpler and avoid having one more hidden field.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.39954-616440</guid>
		<pubDate>Sun, 11 Jun 2006 16:54:27 -0800</pubDate>
		<dc:creator>AmbroseChapel</dc:creator>
	</item><item>
		<title>By: jellicle</title>
		<link>http://ask.metafilter.com/39954/PHP-same-trivial-code-behaving-differently-in-two-places#616444</link>	
		<description>AmbroseChapel - it is using both, which is perfectly legal, but I do think the poster is confusing them.&lt;br&gt;
&lt;br&gt;
&amp;lt;form name=&quot;input&quot; action=&quot;testpost.php?answer=1&quot; method=&quot;post&quot;&amp;gt;&lt;br&gt;
&amp;lt;input name=&quot;submit&quot; type=&quot;submit&quot; value=&quot;GoFirst&quot;&amp;gt;&lt;br&gt;
&amp;lt;/form&amp;gt;&lt;br&gt;
&lt;br&gt;
When the above form is submitted, you end with a GET variable of &quot;answer&quot;, equal to &quot;1&quot;, and a POST variable of &quot;submit&quot;, equal to &quot;GoFirst&quot;.  The GET variable exists because the ACTION URL includes a question mark followed by the string answer, an equals sign, and a 1.  The POST variable exists because the input field in the form is named submit and has a value of GoFirst.&lt;br&gt;
&lt;br&gt;
If and only if register_globals is set, these are available in PHP as &quot;$answer&quot; and &quot;$submit&quot;.&lt;br&gt;
&lt;br&gt;
Whether or not register_globals is set, they are available in PHP as &quot;$_GET[&apos;answer&apos;]&quot; and &quot;$_POST[&apos;submit&apos;]&quot;.&lt;br&gt;
&lt;br&gt;
Try including this in all your test forms for a while:&lt;br&gt;
&lt;br&gt;
echo &quot;&amp;lt;br&amp;gt;GET variables:&amp;lt;br&amp;gt;&quot;;&lt;br&gt;
var_dump($_GET);&lt;br&gt;
echo &quot;&amp;lt;br&amp;gt;POST variables:&amp;lt;br&amp;gt;&quot;;&lt;br&gt;
var_dump($_POST);&lt;br&gt;
&lt;br&gt;
Hopefully that will clear things up for you.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.39954-616444</guid>
		<pubDate>Sun, 11 Jun 2006 17:05:15 -0800</pubDate>
		<dc:creator>jellicle</dc:creator>
	</item><item>
		<title>By: AmbroseChapel</title>
		<link>http://ask.metafilter.com/39954/PHP-same-trivial-code-behaving-differently-in-two-places#616449</link>	
		<description>One more post?&lt;br&gt;
&lt;br&gt;
The thing that&apos;s making it work differently on the server and your computer is a security thing.&lt;br&gt;
&lt;br&gt;
The server is less secure than your computer, because they changed the defaults a while back for that register_globals thing.&lt;br&gt;
&lt;br&gt;
Slightly silly example, but pretend you&apos;ve got a GET form for me to buy something from an online store.&lt;br&gt;
&lt;br&gt;
Your form sends your server this:&lt;br&gt;
&lt;br&gt;
&lt;strong&gt;buy.php?item=t-shirt&amp;amp;price=$10&lt;/strong&gt;&lt;br&gt;
&lt;br&gt;
and your script does this:&lt;br&gt;
&lt;br&gt;
&lt;strong&gt;&lt;em&gt;looks up my credit card details;&lt;br&gt;
puts the number into $credit_card_number;&lt;br&gt;
gets the details from the form and charges my card the $10;&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;br&gt;
if register_globals is switched on, I could hack the form by hand like this:&lt;br&gt;
&lt;br&gt;
&lt;strong&gt;buy.php?item=t-shirt&amp;amp;price=$10&amp;amp;credit_card_number=1234567890&lt;/strong&gt;&lt;br&gt;
&lt;br&gt;
and my hand-crafted credit card number would over-write yours from the database.&lt;br&gt;
&lt;br&gt;
What they&apos;ve done is put all fields derived from forms in a little locked box, so my hacked variable would end up in $_GET[&apos;credit_card_number&apos;] and not clobber yours from the database.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.39954-616449</guid>
		<pubDate>Sun, 11 Jun 2006 17:09:55 -0800</pubDate>
		<dc:creator>AmbroseChapel</dc:creator>
	</item><item>
		<title>By: lockedroomguy</title>
		<link>http://ask.metafilter.com/39954/PHP-same-trivial-code-behaving-differently-in-two-places#616450</link>	
		<description>Khalad, thanks for pointing out the error. I didn&apos;t actually make that error - it&apos;s correct in the code, but for some reason an = got dropped in the posting. (I just couldn&apos;t let everyone here think I got EVERYTHING wrong.)</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.39954-616450</guid>
		<pubDate>Sun, 11 Jun 2006 17:11:06 -0800</pubDate>
		<dc:creator>lockedroomguy</dc:creator>
	</item><item>
		<title>By: lockedroomguy</title>
		<link>http://ask.metafilter.com/39954/PHP-same-trivial-code-behaving-differently-in-two-places#616464</link>	
		<description>Thanks all, it&apos;s all helping.&lt;br&gt;
&lt;br&gt;
Thanks for the clarification Jellicle. I think you&apos;re right about me mixing up GET and POST. I thought that the Method=post was applying to the Action= on the same line, but you&apos;re saying one can be GET, even if I don&apos;t have a Method of GET.  I really did not (and do not) understand that.&lt;br&gt;
&lt;br&gt;
I&apos;ll read up on the suggested links from cerbous, and AmbroseChapel&apos;s examples, and I&apos;ll insert the variable dump suggested by Jellice until I do understand it.&lt;br&gt;
&lt;br&gt;
Thanks all.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.39954-616464</guid>
		<pubDate>Sun, 11 Jun 2006 17:32:39 -0800</pubDate>
		<dc:creator>lockedroomguy</dc:creator>
	</item><item>
		<title>By: ddf</title>
		<link>http://ask.metafilter.com/39954/PHP-same-trivial-code-behaving-differently-in-two-places#616490</link>	
		<description>Unrelated to the GET/POST confusion, you don&apos;t need to use three switch statements. One will do. Furthermore, you don&apos;t need $switchflag, $answer can be used. Your code really boils down to this:&lt;br&gt;
&lt;pre&gt;&lt;br&gt;
if ( !isset($_GET[&apos;answer&apos;]) ) $answer = 0;&lt;br&gt;
else $answer = $_GET[&apos;answer&apos;];&lt;br&gt;
&lt;br&gt;
switch($answer)&lt;br&gt;
{&lt;br&gt;
  case 0:&lt;br&gt;
         // do stuff&lt;br&gt;
         break;&lt;br&gt;
  case 1:&lt;br&gt;
        //do stuff&lt;br&gt;
        break;&lt;br&gt;
  case 2:&lt;br&gt;
        //do stuff&lt;br&gt;
        break;&lt;br&gt;
  default:&lt;br&gt;
       // catch anything that isn&apos;t the first three, &lt;br&gt;
      //  probably do nothing&lt;br&gt;
}&lt;br&gt;
&lt;/pre&gt;</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.39954-616490</guid>
		<pubDate>Sun, 11 Jun 2006 18:27:20 -0800</pubDate>
		<dc:creator>ddf</dc:creator>
	</item><item>
		<title>By: devilsbrigade</title>
		<link>http://ask.metafilter.com/39954/PHP-same-trivial-code-behaving-differently-in-two-places#616556</link>	
		<description>&lt;strong&gt;AmbroseChapel&lt;/strong&gt;, your example of the security problem is actually wrong. Its only an issue if it isn&apos;t initialized somewhere in the code. register_globals won&apos;t overwrite variables you initialize yourself... the danger is something like (in pseudocode, its been a long time since I&apos;ve used php) &lt;br&gt;
&lt;br&gt;
if($cc_number != null)&lt;br&gt;
-- use_cc_number();&lt;br&gt;
else&lt;br&gt;
-- $cc_number = get_cc_from_db();&lt;br&gt;
&lt;br&gt;
if you just do&lt;br&gt;
$cc_number = get_cc_from_db();&lt;br&gt;
$cc_number will be overwritten no matter what (unless PHP has exceptions now), and you&apos;re &apos;safe.&apos;</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.39954-616556</guid>
		<pubDate>Sun, 11 Jun 2006 19:57:32 -0800</pubDate>
		<dc:creator>devilsbrigade</dc:creator>
	</item><item>
		<title>By: AmbroseChapel</title>
		<link>http://ask.metafilter.com/39954/PHP-same-trivial-code-behaving-differently-in-two-places#616562</link>	
		<description>If you say so. I didn&apos;t actually test it, as you can probably tell.&lt;br&gt;
&lt;br&gt;
It seems lockedroomguy is still a little confused though:&lt;br&gt;
&lt;strong&gt;&lt;em&gt;&lt;br&gt;
I thought that the Method=post was applying to the Action= on the same line&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;br&gt;
It is. That&apos;s exactly what your form does. If it was just &lt;br&gt;
&lt;br&gt;
&amp;lt;form action=&quot;script.php&quot; method=&quot;post&quot;&amp;gt;&lt;br&gt;
&lt;br&gt;
 it would send only post values. If it was just &lt;br&gt;
&lt;br&gt;
&amp;lt;form action=&quot;script.php&quot; method=&quot;get&quot;&amp;gt; &lt;br&gt;
&lt;br&gt;
it would send only get values. But it&apos;s an unusual type of form, because it&apos;s &lt;br&gt;
&lt;br&gt;
&amp;lt;form action=&quot;script.php&lt;strong&gt;?foo=bar&lt;/strong&gt;&quot; method=post&quot;&amp;gt; &lt;br&gt;
&lt;br&gt;
and so it combines both.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.39954-616562</guid>
		<pubDate>Sun, 11 Jun 2006 20:07:12 -0800</pubDate>
		<dc:creator>AmbroseChapel</dc:creator>
	</item><item>
		<title>By: Khalad</title>
		<link>http://ask.metafilter.com/39954/PHP-same-trivial-code-behaving-differently-in-two-places#616582</link>	
		<description>It may be enlightening to see what&apos;s going on under the covers, at the HTTP-protocol level. Or it might be even more confusing, but let&apos;s try it and see!&lt;br&gt;
&lt;br&gt;
When you visit a URL, or submit a form, your browser sends an HTTP request to the web server, which looks like this:&lt;br&gt;
&lt;br&gt;
&lt;code&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;POST&lt;sup&gt;1&lt;/sup&gt; /script.php&lt;sup&gt;2&lt;/sup&gt;?foo=bar&lt;sup&gt;3&lt;/sup&gt;&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;baz=quux&amp;amp;answer=2&lt;sup&gt;4&lt;/sup&gt;&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
&lt;b&gt;1&lt;/b&gt; - The request method. Almost always GET or POST. If POST, then after this line you have a blank line followed by the POST variables, which in PHP end up in &lt;code&gt;$_POST&lt;/code&gt;. If the method is GET you can refresh the page at will; if it&apos;s POST, your browser will pop up an &quot;Are you sure you want to resubmit your data?&quot; confirmation box.&lt;br&gt;
&lt;br&gt;
&lt;b&gt;2&lt;/b&gt; - The request URL. Obvious.&lt;br&gt;
&lt;br&gt;
&lt;b&gt;3&lt;/b&gt; - The query string (optional). This is where the &lt;code&gt;$_GET&lt;/code&gt; array comes from. You can have a query string with both a GET and POST request; in the latter case, &lt;code&gt;$_GET&lt;/code&gt; will be filled out with the variables from the query string even though the request method is POST.&lt;br&gt;
&lt;br&gt;
&lt;b&gt;4&lt;/b&gt; - The POST data. This data is only sent for POST requests (duh).&lt;br&gt;
&lt;br&gt;
...&lt;br&gt;
&lt;br&gt;
How does this relate to your web page? Well, the &lt;code&gt;method&lt;/code&gt; attribute on your &lt;code&gt;&amp;lt;form&amp;gt;&lt;/code&gt; element controls &lt;b&gt;1&lt;/b&gt;, the request method. The &lt;code&gt;action&lt;/code&gt; attribute sets &lt;b&gt;2&lt;/b&gt;, the request URL, and optionally &lt;b&gt;3&lt;/b&gt;, the query string.&lt;br&gt;
&lt;br&gt;
If the &lt;code&gt;method&lt;/code&gt; is &lt;code&gt;&quot;get&quot;&lt;/code&gt; then &lt;br&gt;
your form data will be tacked onto the query string and will be available in the &lt;code&gt;$_GET&lt;/code&gt; array in your PHP script. If it&apos;s &lt;code&gt;&quot;post&quot;&lt;/code&gt; then your form data will be sent in &lt;b&gt;4&lt;/b&gt;, the POST data.&lt;br&gt;
&lt;br&gt;
In either case, anything you manually put in the query string will be available in &lt;code&gt;$_GET&lt;/code&gt;.&lt;br&gt;
&lt;br&gt;
&lt;b&gt;Tip:&lt;/b&gt; If you want to see what data ended up where, use &lt;code&gt;var_dump&lt;/code&gt;. You can see all the GET and POST data by typing:&lt;br&gt;
&lt;br&gt;
&lt;code&gt;var_dump($_GET); var_dump($_POST);&lt;/code&gt;</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.39954-616582</guid>
		<pubDate>Sun, 11 Jun 2006 21:24:32 -0800</pubDate>
		<dc:creator>Khalad</dc:creator>
	</item>
	</channel>
</rss>
