doog1092 2013-11-27 22:23
浏览 237
已采纳

为什么我的变量在函数内部未定义

It gives me this error Notice: Undefined variable: newfile and of course it also gives me fwrite() expects parameter 1 to be resource, null given cause of the first error. What I doing wrong?

$file = fopen("mycsv.csv", 'r');
$newfile = fopen("myjson.txt", 'w');

function write($text, $tab) {
    $tabs = "";
    for ($index = 0; $index < $tab; $index++) {
        $tabs .= "\t";
    }
    fwrite($newfile, $text."
".$tabs); //error here
}
  • 写回答

4条回答 默认 最新

  • dongpu6141 2013-11-27 22:26
    关注

    Have a read about variable scope in the manual

    $file and $newfile are global, therfore $newfile cannot be accessed locally in your function. Either move it into the function, pass it in as a parameter or as a last resort declare it as global in the function

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试