Jump to content

[SOLVED] Getting http (url) passed variables


alecks

Recommended Posts

I need to clear this up in my mind, because I have heard so many conflicting answers from online references:

What is the proper way to retrieve HTTP passed variables (http://www.test.com/index.php?var=foo) within a PHP script? (so that it will work with PHP 6 (register_globals gone))

No...

 

$_POST is when your form method is post.

$_GET is when your form method is get.

 

All variables are confined to a function if they are created in one, unless you use global to change it from a local variable (in one function) to a global variable (used in the whole script).

 

<?php
function localVar(){
   $localvar = "local";
   echo "localVar : " . $localvar;
   echo "<br />";
}
function globalVar(){
   global $globalvar;
   $globalvar = "global";
   echo "globalVar : " . $globalvar;
   echo "<br />";
}
localVar();
globalVar();
echo $globalvar . " | " . $localvar;
?>

 

outputs

 

localVar : local

globalVar : global

global |

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.