duandingqi9442 2016-06-29 20:25
浏览 61

函数中全局数组的范围[重复]

I'm trying to add to, and print the contents of a global array that is being accessed within an individual function.

PHP

<?php

// Globals for data cache
$city_array = [];

/*  printArray
 *  print the value of global array
*/
function printArray() {
    print_r($city_array);
}

printArray();

?>

This is returning an error:

Notice: Undefined variable: city_array in /Applications/XAMPP/xamppfiles/htdocs/donorsearch/process.php on line 6

How can I get access to this global array within this local function?

</div>
  • 写回答

2条回答 默认 最新

  • duanjia4969 2016-06-29 20:27
    关注

    To access global variable in function you must use global to tell PHP you want that:

    function printArray() {
        global $city_array;
    
        ....
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题