PHP website development variable scope: Read the PHP website development variable scope. 1. php does not have global static variables. In previous. Net development, some data can be cached using the following method: viewplaincopytoclipboardprint? PublicclassTest {privatestaticintCount = 0; //
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?
$ Url = "www.webjx.com ";
Function _ DisplayUrl (){
Echo $ url;
}
Function DisplayUrl (){
Global $ url;
Echo $ url;
}
_ DisplayUrl ();
DisplayUrl ();
?>
$ 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?
Function _ DisplayUrl (){
Global $ myName;
$ MyName = 'ibin ';
}
_ DisplayUrl ();
Echo $ myName; // output yibin
?>