php php中的会话变量和全局变量有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14848145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between session variables & Global variables in php?
提问by Srividhya
What is the difference between session & Global variables in php?
php中的会话变量和全局变量有什么区别?
回答by Smita
Global variablesare the variables which remain common for the whole application… Their value can be used across the whole application whereas Session variablesare variables which remain common for the whole application but for one particular user. They also can be used across the whole application… But they die when a particular user session ends.
全局变量是对整个应用程序保持通用的变量……它们的值可以在整个应用程序中使用,而会话变量是对整个应用程序但对一个特定用户保持通用的变量。它们也可以在整个应用程序中使用……但是当特定用户会话结束时它们就会消失。
回答by T30
globalis just a keyword to access a variable that is declared in the top level scope and isn't available in the actual scope. This hasn't anything to do with the session: do not persist between pages.
global只是访问在顶级范围中声明的变量的关键字,而在实际范围中不可用。这与会话没有任何关系:不要在页面之间持久化。
$a = "test";
function useGlobalVar(){
echo $a; // prints nothing, $a is not availabe in this scope
global $a;
echo $a; // prints "test"
}
$GLOBALSis another way to access top-level scope variables without using the globalkeyword:
$GLOBALS是另一种不使用global关键字访问顶级范围变量的方法:
$a = "test";
function useGlobalVar(){
echo $GLOBAL['a']; // prints "test"
}
There's a bit of of confusion between globaland superglobals: Superglobals(like $GLOBALS, $_REQUEST, $_SERVER) are available in any scope without you having to make a global declaration. Again, they do not persist between pages (with the exception of $_SESSION).
global和之间存在一些混淆superglobals:超全局变量(如 $GLOBALS、$_ REQUEST、$_SERVER)可在任何范围内使用,而无需进行全局声明。同样,它们不会在页面之间持续存在($_SESSION 除外)。
$_SESSION is a Superglobal array that persist across different pages.
$_SESSION 是一个跨不同页面持久化的超全局数组。
回答by Tucker
Session variables are variables stored server side that persist for a given client connection.
会话变量是存储在服务器端的变量,对于给定的客户端连接持续存在。
global variables are variables that have a universal (global...) scope in your php code. these variables are not necessarily dependent on a given client connection
全局变量是在 php 代码中具有通用(全局...)范围的变量。这些变量不一定依赖于给定的客户端连接
for sessions see: http://www.php.net/manual/en/book.session.php
会议见:http: //www.php.net/manual/en/book.session.php
for global varialbes see: http://www.tutorialspoint.com/php/php_global_variables.htm
全局变量见:http://www.tutorialspoint.com/php/php_global_variables.htm
lastly, this type of question isn't the most appropriate for this forum, see: https://stackoverflow.com/faq#dontask
最后,这类问题不适合本论坛,请参阅:https: //stackoverflow.com/faq#dontask
"You should only ask practical, answerable questions based on actual problems that you face. Chatty, open-ended questions diminish the usefulness of our site and push other questions off the front page.
“你应该只根据你面临的实际问题提出实用的、可回答的问题。喋喋不休、开放式的问题会降低我们网站的实用性,并将其他问题从头版推开。
Your questions should be reasonably scoped. If you can imagine an entire book that answers your question, you're asking too much.
你的问题应该有合理的范围。如果您可以想象一整本书都可以回答您的问题,那么您的要求就太多了。
If your motivation for asking the question is “I would like to participate in a discussion about ______”, then you should not be asking here. However, if your motivation is “I would like others to explain ______ to me”, then you are probably OK. (Discussions are of course welcome in our real time web chat.)"
如果您提出问题的动机是“我想参与有关______的讨论”,那么您不应该在这里提问。但是,如果您的动机是“我希望其他人向我解释______”,那么您可能没问题。(当然欢迎在我们的实时网络聊天中进行讨论。)”
回答by Ja?ck
Global variables are any variable that's declared outside of any function or class scope and are used inside another function by using the globalkeyword, e.g.
全局变量是在任何函数或类范围之外声明并通过使用global关键字在另一个函数内使用的任何变量,例如
$a = 123; // this is a global variable
function foo()
{
global $a; // and this is the explicit declaration
}
Super globals are like regular globals, except that they're implicitly declared global within functions so that they're always available.
超级全局变量与常规全局变量类似,不同之处在于它们在函数中隐式声明为全局变量,因此它们始终可用。
Lastly, session variables are accessible via the super global $_SESSIONand are perpetuated by sending and accepting a session identifier.
最后,会话变量可通过超级全局访问,$_SESSION并通过发送和接受会话标识符而永久存在。
回答by Bhuppi
global variables are those variables that are accessible inside your all php file and php defines some of the global variables which are available to all php scripts. Ex - $_POST , $_SESSION , $_REQUEST .
全局变量是那些可以在所有 php 文件中访问的变量,php 定义了一些可用于所有 php 脚本的全局变量。例如 - $_POST , $_SESSION , $_REQUEST 。
global is also a keyword which is used when you want to access a variable defined outside the function.
global 也是一个关键字,当您要访问在函数外部定义的变量时使用。
<?php
$name = "xyz" ;
function hello(){
global $name ;
echo $name ;
}
?>

