21

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

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

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Captain Comic
  • 15,744
  • 43
  • 110
  • 148

3 Answers3

37

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.

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.

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 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. 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
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • Great explanation. I am .NET/C++ developer so I was thinking in terms of typical C++ program. – Captain Comic Feb 26 '11 at 11:27
  • ***"knowing nothing of the previous call"*** that may confuse even more the asker because you didn't mention nothing about $SESSION. As your answer was to clarify a basic thing, mention it would help asker to understanding what does and what doesn't. – m3nda Nov 11 '15 at 07:26
7

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:

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

still_dreaming_1
  • 8,661
  • 6
  • 39
  • 56
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • You are using the word block differently from the question, although the answer is still correct for the question despite this confusion... – still_dreaming_1 Jul 13 '14 at 15:03
6

Even if $user_info is not declared as global, it can be used in several PHP-blocks : what you posted should work ;-)


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.

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


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.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • But what if someone changes $user_info between two PHP block? What about multithreading issues? – Captain Comic Feb 26 '11 at 10:24
  • 1
    @Captain Well the latter your question means that answer was not that clear. – Your Common Sense Feb 26 '11 at 10:33
  • 2
    @Captain : "someone" ? Your script is executed by one PHP process, you don't have two people executing the same PHP at the same time and in the same memory. If two people execute the script at the same time, the variables of the first execution will be in one memory block, and the variables of the second execution in a second, distinct, memory block => there can be no multithreading issue (PHP doesn't use threads, anyway) – Pascal MARTIN Feb 26 '11 at 10:38