The use of globals variables in PHP

Sep 20, 2007 15:45 GMT  ·  By

PHP programming language provides you a simple way to define functions and variables. The concept of PHP variables is complex in ensemble, but for start let's say that variables used inside PHP code begin with the $ sign and do not need to be initialised.

You must make the difference between older versions of PHP, when variables had a global character and the newest PHP versions in which variables are super globals.

For example, older PHP scripts, made before the apparition of PHP 4.2 version are not using super global definition of variables and as a consequence they will not work on the new versions of PHP, because the default setting in PHP configuration file (PHP.ini) is register_globals off.

You should not be surprised that when you test an older script you cannot run it, but it is a solution for this problem: turn register_globals on in php.ini by modifying the appropriate line. If you use an online webserver, then this setting is not recommended from security reasons.

If you are a PHP coder and use old PHP templates, you should consider rewriting them in order to be compatible with actual Apache and PHP distributions. You can check the script requirements to see if it requires the register_globals to be on. Depending on the webhosting service, there are still situations when register_globals is enabled in php.ini file.

If you do not have access to the server configuration files you can add a .htaccess file to the root directory of your PHP script in order to be able to use scripts that require register_globals on. The .htaccess file must contain the line

code
php_admin_flag register_globals on
Another way to enable the register_globals on is to use the function ini_set() inside your PHP scripts. You can check the register_globals status (on or off) by using php_info() function or the appropriate option in your web account control panel. In order to have applications easy to maintain, portable, secure and with an optimal performance it is recommended to maintain the register_globals off setting and to convert the old code to the actual specifications of PHP coding standards.