PHP-全局变量和常量有什么区别

发布于 2024-08-18 11:21:42 字数 112 浏览 6 评论 0原文

根据许多来源,register_globals(即全局变量)应该在 php.ini 中禁用。 如果全局变量被禁用,我应该在代码中编写define()并使用常量吗?这些是否相关?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

木緿 2024-08-25 11:21:42

它们之间的相关性在于它们具有全局作用域,但常量一旦定义就不会更改,这与页面可以在运行过程中修改的全局变量不同。因此,仅仅切换到使用 Define() 而不是全局变量并没有多大帮助。

最好重构方法以将变量作为参数并依赖它来传递变量。

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.

無處可尋 2024-08-25 11:21:42

全局变量不是常量(您可以更改全局变量的值,但只能定义一次常量)。

常量并不总是全局的(您可以在类中声明常量)。

此外,全局变量可以是任何类型:标量、数组或对象。常量只能是标量。

我不会说常量或全局变量是好还是坏。如果使用得当,它们都有有益的用途。 register_globals 存在安全问题与全局变量的更一般用途分开的功能。

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.

无需解释 2024-08-25 11:21:42

这里有几件事。

首先,您在 php.ini 中禁用的 register_globals 指的是旧的 PHP 功能,其中通过查询字符串 (GET) 或表单 (GET/POST) 发送的任何变量都将转换为全局 PHP 变量。当您关闭 register_globals 时,该功能将被(并且应该)禁用。即使关闭此选项,您仍然可以在应用程序中定义全局变量。

在一般编程术语中,全局变量(不是 PHP 的 register_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.

生生漫 2024-08-25 11:21:42

常量一旦定义就不能更改。

不要将常量用作变量。如果需要在函数内使用变量,请将它们传递到函数本身。按照预期的方式使用所有东西。变量是变量,常量是常量

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.

单调的奢华 2024-08-25 11:21:42

一些不变的例子:

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

?>

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

?>
心清如水 2024-08-25 11:21:42

还有一些需要考虑的事情——常量不能存储数组或对象之类的东西,而定义到 $GLOBALS 的东西可以是任何变量类型。因此,在某些情况下,如果您需要全局的东西,但无法使用 Define() 将其存储为常量,您可能需要使用 $GLOBALS 来代替。

另外,register_globals 和 $GLOBALS 不是同一件事!

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!

秉烛思 2024-08-25 11:21:42

如果全局变量具有相同的名称,则可以更改函数内部的全局变量,因为局部变量会覆盖全局变量,但不会更改全局变量的值。如果您想在不允许的不同函数中使用相同名称的变量,则外部常量不会更改给你并给出一个错误,因为它定义了一次并在所有程序中使用,你不能在任何函数或块中更改此变量的值,它是固定值。

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 .

溺孤伤于心 2024-08-25 11:21:42

尝试这个简单的测试:

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";
?>

然后运行 ​​fileB.php,您将看到在包含 fileA.php 之前,SOMEVAL 未定义。所以这意味着在定义任何内容之前,它对脚本来说是不可见的。

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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文