Home  >  Article  >  Backend Development  >  关于全局global 的有关问题

关于全局global 的有关问题

WBOY
WBOYOriginal
2016-06-13 13:28:37684browse

关于全局global 的问题
当我访问a.php页面的时候 调用b.php中的一个方法,这个方法是执行采集,需要一段时间才能采集完,

我在a页面中有个全局的变量 $c=""; b页面中给$c赋值一次,然后供其他方法使用$c。

如果我再次访问a页面的时候这个$c的值又变为了 初始值空,这个会对b页面中的$c产生影响吗??
(a页面只执行一次b中的方法,当b的方法执行完 a才回再次触发b页面的方法)



我写了个小的demo测试,不知道表述的是否准确,希望有人可以给指点下或者明确下:

PHP code
//写进日志
function write_logs($str='') {
global $num;
if($num>1){
    $num="ok";
}
    $fliepath_dir="test.txt";
    $str=$str."-".$num."\r\n";
    if($fp = @fopen($fliepath_dir, 'a')) {
        @flock($fp, 2);
        fwrite($fp, $str);
        fclose($fp);
        return true;
    }else{
        return false;
    }
}
$num=0;
$act=$_GET["act"];
if($act=="b"){
$num="2";
for ($index = 1; $index 


访问上面这个文件 传递act=b 的时候 才运行写入$num=2;
然后 不传递参数就$num=0; 恢复初始值
然后 在日志里判断 $num是否大于1 即赋值是否成功
这样写应该能测试 上面我说的那种情况吧?


------解决方案--------------------
可以,验证了
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