English 中文(简体)
PHP -What s the difference between global variables and constants
原标题:

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

最佳回答

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

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

问题回答

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

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

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

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

A few things here.

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

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

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

Constants, once defined, cannot be changed.

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

Some constant 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 );

?>

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

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

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

Try this simple test:

fileA.php:

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

fileB.php:

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

Then run fileB.php and you ll see that before you include fileA.php, SOMEVAL is not defined. So what this means is that before you define anything, it won t be visible to the script.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签