加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

如何在PHP中将数组声明为全局?

发布时间:2020-12-13 17:20:36 所属栏目:PHP教程 来源:网络整理
导读:每次加载页面时,我都需要将数组的内容写入文件… 我已经在index.php中创建了数组,并将内容推送到另一个ajax页面中的数组. 但是我无法全局访问数组..它显示错误为’undefined variable $arr’.. 这是我的代码.. Index.php page...?php $arr = array(); $ourFi
每次加载页面时,我都需要将数组的内容写入文件…
我已经在index.php中创建了数组,并将内容推送到另一个ajax页面中的数组.
但是我无法全局访问数组..它显示错误为’undefined variable $arr’..

这是我的代码..

Index.php page...

<?php
    $arr = array();

    $ourFileName = "saved_data.txt";
    $ourFileHandle = fopen($ourFileName,'w') or die("can't open file");
    fwrite($ourFileHandle,"");

?>

Ajax页面…..

<?php
    $name_full = $_GET['name_full'];
    $arr = $_GET['$arr'];

    array_push($arr,$name_full);
    /*------------To create a file-----------------*/
    $ourFileName = "saved_data.txt";
    $ourFileHandle = fopen($ourFileName,'w') or die("can't open file");
    /*---------To save data in the file------------*/
    foreach($arr as $key => $value)
    {
        fwrite($ourFileHandle,$value);
    }
    fwrite($ourFileHandle,',');
    fclose($ourFileHandle);
    echo $name_full;
?>

还有什么办法让这个数组全局化……

解决方法

在ajax页面中声明$arr as,global $arr;并看看这是否有效,但我怀疑这不起作用,因为每次加载页面时,数组都会重置,为什么不使用会话?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读