PHP website development variable scope _ PHP Tutorial

Source: Internet
Author: User
Tags php website
PHP website development variable scope. 1. there is no global static variable in php. In previous. Net development, some data can be cached using the following method: viewplaincopytoclipboardprint? PublicclassTest {priva

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 = yibin;
}

_ DisplayUrl ();
Echo $ myName; // output yibin
?>

Bytes. In previous. Net development, some data can be cached using the following method: view plaincopy to clipboardprint? Public class Test {priva...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.