Variable scope in PHP website development. 1. PHP has no global static variables. NET was previously developed. you can use the following method to cache some data: viewplaincopytoclipboardprint? PublicclassTest {private
1. there is no global static variable in PHP.
In previous. NET development, you can use the following method to cache some data:
View plaincopy to clipboardprint?Public class Test { Private static int Count = 0; // This variable is valid throughout the application. } Public class Test { Private static int Count = 0; // This variable is valid throughout the application. } |
PGP is an interpreted language. although there is a static modifier, the meaning is completely different from that in. NET.
Even if you declare a variable in the class as static, this variable is only valid in the current page-level application domain.
2. understand the scope of variables
Variables declared in vitro of the method cannot be accessed in the method body.
For example:
view plaincopy to clipboardprint?
$url = "www.51cto.com"; function _DisplayUrl() { echo $url; } function DisplayUrl() { global $url; echo $url; } _DisplayUrl(); DisplayUrl(); ?>
$url = "www.51cto.com"; function _DisplayUrl() { echo $url; } function DisplayUrl(){ global $url; echo $url; } _DisplayUrl(); DisplayUrl(); ?> |
The _ DisplayUrl method does not display any results, because the variable $ url cannot be accessed in the method body _ DisplayUrl. you can add global before $ url, such as the DisplayUrl method.
Global variables defined in the method body can be accessed in the method body in vitro:
view plaincopy to clipboardprint?
function _DisplayUrl() { global $myName; $myName='yibin'; } _DisplayUrl(); echo $myName; //output yibin ?> |
What you are reading is "variable scope in PHP website development"
- PHP Developer: Have you finished GLAMMP?
- 10 common PHP functional codes
- PHP file upload ideas and examples
In the previous. NET development of idea, you can use the following method to cache some data: view plaincopy to clipboardprint? Public class Test {private...