P粉6172377272023-08-25 09:32:59
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粉4669094492023-08-25 09:01:17
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: