代码块之间的PHP变量范围 [英] PHP variable scope between code blocks

查看:137
本文介绍了代码块之间的PHP变量范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PHP的新手(仍然),并且继续学习.

I am new to PHP (still) and keep learning.

我经常不得不检索某个变量并访问其属性.

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

我希望一次检索$user_info = get_userdata($id);,然后在需要时使用它 在同一文件中但在不同的<?php?>块中

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

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

但是我怀疑$user_info不能在块之间共享,因为它不是全局的. 通常的做法是什么?

But I suspect $user_info cannot be shared between blocks because it is not global. What is usual practice for that?

推荐答案

您在php代码块中投入了过多的含义.
这不是全球性的事情.
这些块属于相同的PHP脚本.这只是输出HTML的一种简洁方法,仅此而已.您可以用回显HTML代替它,不会有丝毫差别.

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块在客户端执行,然后又返回到服务器上的PHP块侧面等等.错了.
整个PHP脚本在服务器端执行,在浏览器中生成纯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.

这就是为什么不能通过仅将HTML表单及其处理程序放在前一个PHP脚本的后面,而无法在同一PHP脚本中对HTML表单及其处理程序进行编程的原因.您必须再次调用服务器,以使处理程序正常工作.这将完全是另一个调用,是同一脚本的另一个实例,对之前的调用一无所知,该调用早已死了.这是您必须了解的关于PHP的另一件事:

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 server to make the handler work. It will be another call completely, another instance of the same script, knowing nothing of the previous call which is long dead already. And that's another thing you have to know about PHP:

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:

  1. 浏览器拨打电话
  2. PHP唤醒,创建HTML页面,将其发送到浏览器
  3. 浏览器呈现HTML并将其显示给用户.
  4. 用户点击链接
  5. 浏览器拨打电话
  6. 另一个PHP实例,对先前的调用一无所知,会醒来,依此类推
  1. a browser makes a call
  2. PHP wakes up, creates an HTML page, sends it to the browser and dies
  3. Browser renders that HTML and shows it to the user.
  4. User clicks a link
  5. a browser makes a call
  6. another PHP instance, knowing nothing of the previous call, wakes up and so on

这篇关于代码块之间的PHP变量范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆