代码块之间的 PHP 变量范围
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5126261/
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
PHP variable scope between code blocks
提问by Captain Comic
I am new to PHP (still) and keep learning.
我是 PHP 新手(仍然)并继续学习。
I often have to retrieve a certain variable and access its properties.
我经常需要检索某个变量并访问其属性。
<?php
$id = $_REQUEST['id'];
$user_info = get_userdata($id);
echo('Username: ' . $user_info->user_login . "<br>");
echo('User level: ' . $user_info->user_level . "<br>");
echo('User ID: ' . $user_info->ID . "<br>");
echo('First Name: ' . $user_info->user_firstname . "<br>");
echo('Family Name: ' . $user_info->user_lastname . "<br>");
echo('user_registered: ' . $user_info->user_registered . "<br>");
?>
I would prefer to once retrieve $user_info = get_userdata($id);
and then use it when needed
in the same file but in different <?php?>
blocks
我更喜欢一次检索$user_info = get_userdata($id);
,然后在需要时在同一个文件但在不同的<?php?>
块中使用它
<?php
$id = $_REQUEST['id'];
$user_info = get_userdata($id);
?>
<some HTML>
<?php echo $user_info->user_login; ?>
<some HTML>
<?php echo $user_info->user_login; ?>
But I suspect $user_info
cannot be shared between blocks because it is not global.
What is usual practice for that?
但我怀疑$user_info
不能在块之间共享,因为它不是全局的。通常的做法是什么?
回答by Your Common Sense
You're putting too much meaning in the php code blocks.
It's not something that global.
These blocks belong to the same PHP script. It's just a neat way to output HTML, nothing more. You can substitute it with echoing the HTML and there will not be the slightest difference.
您在 php 代码块中赋予了太多意义。
这不是全球性的。
这些块属于同一个 PHP 脚本。这只是输出 HTML 的一种巧妙方式,仅此而已。你可以用回显 HTML 来代替它,不会有丝毫的不同。
The whole PHP script is being executed at once, not in iterations, as you probably picture this, thinking that PHP blocks are being executed server-side, then HTML blocks client-side, and then back to PHP blocks on the server side and so on. That's wrong.
The whole PHP script is being executed on the server side, resulting with pure HTML in the browser, and then dies.
整个 PHP 脚本立即执行,而不是迭代执行,正如您可能想象的那样,认为 PHP 块是在服务器端执行的,然后 HTML 块在客户端执行,然后又回到服务器端的 PHP 块,依此类推在。那是错误的。
整个 PHP 脚本在服务器端执行,在浏览器中生成纯 HTML,然后死亡。
That's why you can't program both an HTML form and its handler in the same PHP script by just placing the latter one right after the former. You have to make another call to the serverto make the handler work. It will be another call completely, another instance of the same script, knowing nothing of the previous callwhich is long dead already. And that's another thing you have to know about PHP:
这就是为什么您不能通过将后者放在前者之后来在同一个 PHP 脚本中同时编写 HTML 表单及其处理程序的原因。您必须再次调用服务器才能使处理程序工作。这将完全是另一个调用,同一个脚本的另一个实例,对之前已经死了很久的调用一无所知。这是您必须了解的有关 PHP 的另一件事:
PHP script execution is atomic. It's not like a desktop application constantly running in your browser, or even a daemon with persistent connection to your desktop application. It's more like a command-line utility - doing its job and exits. It runs discretely:
PHP 脚本执行是原子的。它不像是在浏览器中不断运行的桌面应用程序,甚至不像是与桌面应用程序保持持久连接的守护进程。它更像是一个命令行实用程序 - 完成其工作并退出。它离散地运行:
- a browser makes a call
- PHP wakes up, creates an HTML page, sends it to the browser and dies
- Browser renders that HTML and shows it to the user.
- User clicks a link
- a browser makes a call
- another PHP instance, knowing nothing of the previous call, wakes upand so on
- 浏览器拨打电话
- PHP 唤醒,创建一个 HTML 页面,将其发送到浏览器并死掉
- 浏览器呈现该 HTML 并将其显示给用户。
- 用户点击链接
- 浏览器拨打电话
- 另一个 PHP 实例,对之前的调用一无所知,唤醒等等
回答by Sarfraz
You can use it in blocks (loops, if statements) but you can not use it inside functions. For it to work inside functions, you will have to use the global
keyword:
您可以在块(循环、if 语句)中使用它,但不能在函数中使用它。为了让它在函数内部工作,你必须使用global
关键字:
$user_info ....... //declared outside
function foo(){
global $user_info // now available here too
// more code
}
You can read more about PHP variable scope on the official docs :)
回答by Pascal MARTIN
Even if $user_info
is not declared as global
, it can be used in several PHP-blocks : what you posted should work ;-)
即使$user_info
没有声明为global
,它也可以在几个 PHP 块中使用:您发布的内容应该可以工作 ;-)
The interesting manual page about that is this one : Variable scope; quoting:
有趣的手册页是这个:变量范围;引用:
For the most part all PHP variables only have a single scope.
This single scope spans included and required files as well.
大多数情况下,所有 PHP 变量都只有一个作用域。
这个单一的范围也涵盖了包含和必需的文件。
If the scope spans to other files (but not functions in those files !), it probably spans to distinct php-blocks in the same file, too ;-)
如果范围跨越到其他文件(但不是那些文件中的函数!),它也可能跨越到同一文件中不同的 php 块;-)
Basically, you have :
基本上,你有:
- One global scope : outside of all functions (and variables declared as global, inside functions)
- One scope per function.
- 一个全局作用域:在所有函数之外(以及在函数内部声明为全局的变量)
- 每个函数一个作用域。
You are in the first situation, with your examples.
您处于第一种情况,举个例子。