1. There is no global static variable in PHP.
When I was doing .Net development in the past, 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 static, this variable is only valid in the current page-level application domain.
2. Understand variable scope.
Variables declared outside a method cannot be accessed within the method body.
Such as:
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 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?
<?php
function _DisplayUrl() {
global $myName;
$myName='yibin';
}
_DisplayUrl();
echo $myName; //output yibin
?>