PHP What is the difference between global variables and constants?

According to many sources, register_globals (global variables that are) should be disabled in your php.ini. Should I write define () in my code and use constants if global variables are disabled? Are these related?

+7
variables php constants global
source share
8 answers

They are related by the fact that they have a global scope, but the constants should not change after definition, in contrast to global variables, which a page can change as it moves. Therefore, just switching to using define () instead of global will not help.

Better if you reorganize your methods to take variables as parameters and rely on it to pass variables around.

+15
source share

A few things here.

Firstly, register_globals, which you disabled in php.ini, refers to an old PHP function, where any variable sent via a query string (GET) or form (GET / POST) will be converted to a global PHP variable. This is a function that (and should be) disabled when register_globals is disabled. Even so, you can define global variables in your application.

In general terms of programming, global variables (not PHP register_globals) are considered "bad" because when you encounter one of them as a programmer, you have no idea what other parts of the application can use or modify it or that changes to this global can occur . Also, if you define a new global variable, you can overwrite the existing variable that someone else relies on. When variables are defined locally (in one function or in other languages, one block), you can check the local area and usually determine what the change of this variable will do.

Constants, on the other hand, never change. You define them once, and they remain defined, and no one can change them. This is why global constants are considered "less bad" than global variables.

+8
source share

Global variables are not constants (you can change the value of a global variable, but you can only define a constant once).

Constants are not always global (you can declare a constant in a class).

In addition, any type of global variable can be a scalar, an array, or an object. Constants can only be scalars.

I am not going to say that constants or global changes are good or bad. When used properly, they both have beneficial uses. There are security issues around register_globals that are separate from the more common use of global characters.

+6
source share

Constants, after definition, cannot be changed.

Do not use constants as variables. If you need to use variables inside functions, pass them to the function itself. Use everything as it was intended to be used. Variables are variables and constants are constant.

+2
source share

Some persistent examples:

 <?php // Certainly constant define('MINUTES_PER_HOUR', 60); define('DOZEN', 12); // Constant, but specific to this application define('GREETING', 'Dear %s'); define('TIMEOUT', 30); // Configurable, but constant for this installation define('DATABASE', 'mydb'); define('IMAGES_DIRECTORY', '/tmp/images'); // Not constant, or some other reason why can't be constant $user = $_POST['userid']; $days_of_week = array('Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su'); ?> 
+1
source share

Something else to consider - constants cannot store things like arrays or objects, while something defined for $ GLOBALS can be any type of variable. Therefore, in some cases, if you need something global, but it cannot be stored in a constant using define (), you can use $ GLOBALS instead.

In addition, register_globals and $ GLOBALS are NOT ONLY!

0
source share

You can change the global variable inside the function if both have the same name, since the local variable overrides the global variable, but does not change the value of the global variable outside.in constant if you want to use a variable of the same name in different functions that you are not allowed to and give an error because it defines once and is used throughout the program, and you cannot change the value of this variable in any function or block, this is a fixed value.

0
source share

Try this simple test:

fileA.php:

 <?php define('SOMEVAL', 2); ?> 

fileB.php:

 <?php if(defined('SOMEVAL')) echo SOMEVAL; else echo "SOMEVAL does not exists\n"; include 'fileA.php'; if(defined('SOMEVAL')) echo 'SOMEVAL='.SOMEVAL; else echo "SOMEVAL does not exists\n"; ?> 

Then run the B.php file and you will see that before including the A.php file, SOMEVAL is undefined. So this means that before you define anything, it will not be visible to the script.

-one
source share

All Articles