1
phillipd
What do these "if" sections do?
  • 2004/7/14 16:07

  • phillipd

  • Quite a regular

  • Posts: 219

  • Since: 2004/4/20


I'm really a nooby at PHP. Can someone please tell me what these sections of code do and why they are necessary?

Is $HTTP_GET_VARS specific to XOOPS or just PHP?

if (isset($HTTP_GET_VARS)) {
foreach ($HTTP_GET_VARS as $k => $v) {
$$k = $v;
}
}

if (isset($HTTP_POST_VARS)) {
foreach ($HTTP_POST_VARS as $k => $v) {
$$k = $v;
}
}


2
ackbarr
Re: What do these "if" sections do?

$HTTP_GET_VARS and $HTTP_POST_VARS are arrays managed by PHP that contain variables passed via a querystring (everything after the '?' in an URL) or a form. The code snippet above takes everything that is in both arrays and creates a local variable of the same name.

lets say you have the following URL:
http://www.example.com/myscript.php?a=1&b=2&c=3

$HTTP_GET_VARS contains three elements:

$HTTP_GET_VARS['a'] = 1
$HTTP_GET_VARS['b'] = 2
$HTTP_GET_VARS['c'] = 3

after this code runs:
if (isset($HTTP_GET_VARS)) {
    foreach (
$HTTP_GET_VARS as $k => $v) {
        $
$k $v;
    }
}


3 new variables are created:
$a = 1
$b = 2
$c = 3

Though easy to do, the above loop is considered sloppy coding. No matter what variable is added to the querystring, PHP will automatically create a variable with the same name for you. For security reasons, it is better to explicitly retrieve the variables you need from the $HTTP_GET_VARS array:

$a $HTTP_GET_VARS['a']
$b $HTTP_GET_VARS['b']
$c $HTTP_GET_VARS['c']


To learn more about these and other variables that PHP creates for use in your scripts, I'd recommend checking out PHP.net's documentation on Predefined Variables

Login

Who's Online

801 user(s) are online (67 user(s) are browsing Support Forums)


Members: 0


Guests: 801


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Jul 31
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits