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.
}
Php 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?
<? Php
$ Url = "www.webjx.com ";
Function _ DisplayUrl (){
Echo $ url;
}
Function DisplayUrl (){
Global $ url;
Echo $ url;
}
_ DisplayUrl ();
DisplayUrl ();
?>
<? Php
$ Url = "www.webjx.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?
<? Php
Function _ DisplayUrl (){
Global $ myName;
$ MyName = yibin;
}
_ DisplayUrl ();
Echo $ myName; // output yibin
?>