[ACCEPTED]-PHP -What's the difference between global variables and constants-constants

Accepted answer
Score: 17

They are related in that they have global 8 scope, but constants are meant to not change 7 once defined, unlike global variables which 6 the page can modify as it goes along. So 5 just switching over to using define() instead 4 of a global won't help much.

It's better 3 if you refactor your methods to take the 2 variables as parameters and rely on that 1 to pass variables around.

Score: 10

Global variables aren't constant (you can change the value of a global variable, but 9 you can only define a constant once).

Constants aren't always global (you 8 can declare a constant in a class).

Also, global 7 variables can be any type: scalar, array, or 6 object. Constants can only be scalars.

I'm 5 not going to say either constants or globals 4 are good or bad. When used appropriately, they 3 both have beneficial uses. There are security 2 issues around the register_globals feature that are separate 1 from more general use of globals.

Score: 9

A few things here.

First, the register_globals 27 that you disable in your php.ini refers 26 to an old PHP feature where any variable 25 sent via a query string (GET) or form (GET/POST) would 24 be converted into a global PHP variable. This 23 is the functionality that is (and should 22 be) disabled when you turn off register_globals. Even 21 with this off, you can still define global 20 variables in your application.

In general 19 programming terms, global variables (not 18 PHP's register_globals) are considered "bad" because 17 when you encounter one as a programmer, you 16 have no idea what other parts of the application 15 might be using or changing it, or what effect 14 your changes to that global might have. Also, if 13 you're defining a new global variable, there's 12 a chance you're going to overwriting an 11 existing variable that someone else is relying 10 on. When variables are defined locally (in 9 a single function, or in other languages 8 a single block) you can examine the local 7 scope and usually determine what a change 6 to that variable will do.

Constants, on the 5 other hand, never change. You define them 4 once, and they stay defined and no one can 3 change them. That's why having global constants 2 is considered "less bad" than having global 1 variables.

Score: 2

Constants, once defined, cannot be changed.

Don't 5 use constants as variables. If you need 4 to use variables within functions, pass 3 them into the function itself. Use everything 2 in the way it was intended to be used. Variables 1 are variable and Constants are constant.

Score: 1

Some constant examples:


// 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');



Score: 0

Something else to consider -- constants 7 can't store things like arrays or objects, whereas 6 something defined to $GLOBALS can be any 5 variable type. So in some cases, if you 4 need something to be global but it can't 3 be stored to a constant by using define(), you 2 might want to use $GLOBALS instead.

Also, register_globals 1 and $GLOBALS are NOT the same thing!

Score: 0

You can change the global variable inside 10 the function if both have a same name, because 9 local variable override the global variable 8 but does not change the value of global 7 variable outside.in constant if you want 6 to use same name variable in different function 5 that not allowed to you and give a error,because 4 it define one time and used in all program 3 and you can not change the value of this 2 variable in any function or block it is 1 fixed value .

More Related questions