PHP variable scope across code blocks
P粉771233336
P粉771233336 2023-08-24 13:06:58
0
2
609
<p>I am very new to PHP and am still learning. </p> <p>I often need to retrieve a specific variable and access its properties. </p> <pre class="brush:php;toolbar:false;"><?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('Name: ' . $user_info->user_firstname . "<br>"); echo('Last name: ' . $user_info->user_lastname . "<br>"); echo('Registration time: ' . $user_info->user_registered . "<br>"); ?></pre> <p>I prefer to retrieve<code>$user_info = get_userdata($id);</ code> and then use it when needed. </p> <pre class="brush:php;toolbar:false;"><?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; ?></pre> <p>But I suspect that <code>$user_info</code> cannot be shared between blocks since it is not global. What is the usual approach in this situation? </p>
P粉771233336
P粉771233336

reply all(2)
P粉617237727

You can use it inside code blocks ( loops, conditional statements ), but not inside functions . If you want to use it inside a function, you need to use the global keyword:

$user_info ....... //在外部声明

function foo(){
   global $user_info // 现在也可以在这里使用

   // 更多代码
}

You can learn more about PHP variable scope in the official documentation :)

P粉466909449

You put too much meaning into the php code block.
This is not a global thing.
These blocks belong to the same PHP script. It's just a nice way of outputting HTML and has no other meaning. You can replace it with echo HTML without any difference.

The entire PHP script is executed at once, not in an iterative manner, as you might imagine, think of the PHP block being executed on the server side, then the HTML block being executed on the client side, and then back to the server side to execute the PHP block, So on and so forth. This is wrong.
The entire PHP script is executed on the server side, and the result is displayed in the browser as pure HTML, and then ends.

That's why you can't write both an HTML form and its handler in the same PHP script, just put the latter after the former. You must call the server again for the handler to work. This will be a completely different call, another instance of the same script, knowing nothing about the previous call, which has long since ended. This is also another thing you must know about PHP:

The execution of PHP scripts is atomic. It's not like a desktop application running continuously in the browser, or even a daemon keeping a persistent connection to the desktop application. It's more like a command line utility - do your job and exit. It runs discretely:

  1. Browser initiates call
  2. PHP wakes up, creates an HTML page, sends it to the browserand ends
  3. The browser renders HTML and displays it to the user.
  4. User clicks link
  5. Browser initiates call
  6. Another PHP instance, knowing nothing about the previous call, wakes up and so on
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template