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不能在块之间共享,因为它不是全局的。通常的做法是什么?
3 个解决方案
#1
31
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.
整个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 first one. You will have to make another call to the server to make the handler work. It will be another call completely, another instance of the same script, while the previous one has been long dead - that's another thing you have to know about PHP:
这就是为什么你不能在同一个PHP脚本中编写HTML表单及其处理程序的原因,只需在第一个脚本之后放置后者即可。您将不得不再次调用服务器以使处理程序工作。完全是另一个调用,同一个脚本的另一个实例,而前一个已经很久了 - 这是你必须要了解的另一件事:
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
- PHP醒来,创建一个HTML页面,将其发送到浏览器并死掉
- Browser renders that HTML and shows it to the user.
- 浏览器呈现HTML并将其显示给用户。
- User clicks a link
- 用户单击链接
- a browser makes a call
- 浏览器拨打电话
- another PHP instance, knowing nothing of the previous call, wakes up and so on
- 另一个PHP实例,不知道前一个调用,唤醒等等
#2
6
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 :)
您可以在官方文档上阅读有关PHP变量范围的更多信息:)
#3
5
Even if $user_info
is not declared as global
, it can be used in several PHP-blocks : what you posted should work ;-)
即使$ user_info未被声明为全局,也可以在多个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.
你是第一种情况,你的例子。
#1
31
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.
整个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 first one. You will have to make another call to the server to make the handler work. It will be another call completely, another instance of the same script, while the previous one has been long dead - that's another thing you have to know about PHP:
这就是为什么你不能在同一个PHP脚本中编写HTML表单及其处理程序的原因,只需在第一个脚本之后放置后者即可。您将不得不再次调用服务器以使处理程序工作。完全是另一个调用,同一个脚本的另一个实例,而前一个已经很久了 - 这是你必须要了解的另一件事:
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
- PHP醒来,创建一个HTML页面,将其发送到浏览器并死掉
- Browser renders that HTML and shows it to the user.
- 浏览器呈现HTML并将其显示给用户。
- User clicks a link
- 用户单击链接
- a browser makes a call
- 浏览器拨打电话
- another PHP instance, knowing nothing of the previous call, wakes up and so on
- 另一个PHP实例,不知道前一个调用,唤醒等等
#2
6
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 :)
您可以在官方文档上阅读有关PHP变量范围的更多信息:)
#3
5
Even if $user_info
is not declared as global
, it can be used in several PHP-blocks : what you posted should work ;-)
即使$ user_info未被声明为全局,也可以在多个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.
你是第一种情况,你的例子。