• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1457
  • Last Modified:

PHP 5 and global variables

Since register_globals is turned off by default in PHP 5, I am curious if the following line would not work:
global $_POST
Will the code above work in PHP 5 with register_globals turned off?
3 Solutions
Hi, from the site: http://devzone.zend.com/manual/reserved.variables.html 

HTTP POST variables: $_POST
Note: Introduced in 4.1.0. In earlier versions, use $HTTP_POST_VARS.
An associative array of variables passed to the current script via the HTTP POST method. Automatically global in any scope.

This is a 'superglobal', or automatic global, variable. This simply means that it is available in all scopes throughout a script. You don't need to do a global $_POST; to access it within functions or methods, as you do with $HTTP_POST_VARS.

$HTTP_POST_VARS contains the same initial information, but is not an autoglobal. (Note that $HTTP_POST_VARS and $_POST are different variables and that PHP handles them as such)

If the register_globals directive is set, then these variables will also be made available in the global scope of the script; i.e., separate from the $_POST and $HTTP_POST_VARS arrays. For related information, see the security chapter titled Using Register Globals. These individual globals are not autoglobals.
Loganathan NatarajanLAMP DeveloperCommented:
$_POST is a global variable ... you need not again to define as global variable... do you have any problem in accessing the variable?
Beverley PortlockCommented:
Just to be clear, there is a big difference between the "global" keyword and register_globals - they are NOTHING to do with each other.

Register_globals *creates* variables automatically from the URL query string. It is considered to be the biggest single security flaw in PHP which is why it is off by default. See http://uk2.php.net/register_globals

The "global" keyword makes variables defined outside a function visible inside a function. It is not considered good programming practice to use "global". See http://uk2.php.net/global

Finally there are special arrays that are defined as SUPER GLOBALS that are available in all contexts. These are things like $_POST, $_GET, $_SERVER, $_SESSION and $_COOKIE. See http://uk2.php.net/manual/en/language.variables.superglobals.php
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now