Variable scope between PHP code blocks
P粉403821740
P粉403821740 2023-10-22 14:41:03
0
2
653

I'm (still) new to PHP and learning as I go.

I often need 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 prefer to retrieve $user_info = get_userdata($id); once 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; ?>

But I suspect $user_info cannot be shared between blocks because it is not global. What is the usual approach?

P粉403821740
P粉403821740

reply all(2)
P粉713846879

You can use it inside a block (loop, if statement ), but not inside a function . In order for it to work inside a function you have to use the global keyword:

$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 documentation :)

P粉262926195

You are giving too much meaning in your php code block.
This is not a global thing.
These blocks belong to the same PHP script. It's just a clever way of outputting HTML, nothing more. You can replace it with echo HTML without any difference.

The entire PHP script is executed at once, rather than iteratively, as you might imagine, think of the PHP block executing on the server side, then the HTML block executing on the client side, then back to the PHP block on the server side, etc. This is wrong.
The entire PHP script is executed server-side, generates plain HTML in the browser, and then disappears.

That's why you can't program 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 properly. This will be another call entirely, another instance of the same script, knowing nothing about the previous call, which is long dead. Here's another thing you must know about PHP:

PHP script execution is atomic. It's not like a desktop application that is constantly running in the browser, or even a daemon that is constantly connected to the desktop application. It's more like a command line utility - does its job and exits. It runs independently:

  1. Browser calls
  2. PHP wakes up, creates the HTML page, sends it to the browserthen terminates
  3. The browser renders this HTML and displays it to the user.
  4. User clicks link
  5. Browser calls
  6. Another PHP instance, knowing nothing about the previous call, is awakenedand so on
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!