Home  >  Article  >  Backend Development  >  PHP website development variable scope_PHP tutorial

PHP website development variable scope_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:37:20760browse

1. There is no global static variable in PHP.

When I used to do .Net development, I could 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 it has the static modifier, its meaning is completely different from that in .Net.
Even if a variable in the class is declared as static, this variable is only valid in the current page-level application domain.

2. Understand variable scope.

Variables declared outside the method cannot be accessed within 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 will not display any results because the variable $url is inaccessible in the method body _DisplayUrl. Just add global before $url, such as the DisplayUrl method.

Global variables defined in the method body can be accessed outside the method:

view plaincopy to clipboardprint?
function _DisplayUrl() {
global $myName;
$myName=yibin;
}

_DisplayUrl( );
echo $myName; //output yibin
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486595.htmlTechArticle1. There is no global static variable in PHP. When doing .Net development before, you can use the following method to cache some data: view plaincopy to clipboardprint? public class Test { private...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn