Jump to content

declaring variables


SmokingMan

Recommended Posts

I'm new to PHP and am learning it at home in my spare time. In declaing variables the book gives three formats that can be used. The third one being deprecated "$HTTP_POST_VARS['variable']". The other two are $variable and $_POST['variable'].Does it matter which form is used, or does that depend on the version of PHP being run? The book says the short form requires the register_globals configuration to be on. Is this a default setting or would I need to check with the hosting service as to whether or not this setting is on or not. Or is this something I need not worry about?I'm very new at this so please be patient :)

Link to comment
Share on other sites

The superglobal ($_...) arrays all serve specific functions, such as when sending forms to PHP pages. Have a look at http://www.w3schools.com/php/php_post.asp and http://www.w3schools.com/php/php_get.asp . $variable is just a normal variable, and $HTTP_POST_VARS is just an old name for $_POST. So use $variable.

Link to comment
Share on other sites

Don't count on register_globals being enabled. If you have a variable in an array like $_POST or $_GET, don't just use the variable name alone without getting it from the array first. If you have a form with a field called "username", this code would rely on register_globals:

<?phpif ($username != ""){  ...}?>

So it's going to fail if you move it to any server where register_globals is disabled (many production servers have it disabled). This is the right way to do it, and will work regardless of whether or not register_globals is enabled:

<?php$username = $_POST['username'];if ($username != ""){  ...}?>

It's not important to use the old-style $HTTP_POST_VARS, $HTTP_GET_VARS etc arrays, I've never seen a server running online with a version of PHP that doesn't support $_POST and $_GET.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...