2

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."\n".$tabs); //error here
}
biox
  • 1,526
  • 5
  • 17
  • 28
  • 1
    [read up on variable scope](http://php.net/manual/en/language.variables.scope.php), _**avoid**_ the `global` temptation, and just provide `$newfile` as a parameter to your function. – Wrikken Nov 27 '13 at 22:26

4 Answers4

3

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

WayneC
  • 5,569
  • 2
  • 32
  • 43
  • well I didn't want to pass it as parameter each time I call the function. Why its bad to use global? – biox Nov 27 '13 at 22:31
  • Additionally, if you want a function to affect a variable that you pass as a parameter, then you need to 'pass by reference' rather than 'pass by value'. A better explanation of what that means is [here](http://courses.washington.edu/css342/zander/css332/passby.html). Its not in php, but its the same concept. – echochamber Nov 27 '13 at 22:32
  • 1
    @xoemab global variable usage is bad because you might have variable colisions. This means that lets say you are using code written by somebody else in conjunction with your own. If both of you define the same variable and give it some value, then you are going to have overwritten a variable the other person was expecting not to have been overwritten. This is actually the primary reasons for [namespaces](http://us2.php.net/namespaces) – echochamber Nov 27 '13 at 22:34
0

You need to make $newfile global to be able to use it inside a function's scope:

function write($text, $tab) {
    global $newfile;
    // ... the rest
}
scrowler
  • 24,273
  • 9
  • 60
  • 92
  • 1
    Not good practice. Yes, it solves the problem but a better way to address it is to pass an argument to the function. – symcbean Nov 27 '13 at 22:27
  • 1
    Global variables are generally bad practice for a number of reasons. See this SO link for a good explanation: http://stackoverflow.com/questions/19158339/python-why-are-global-variables-evil – Jake Nov 27 '13 at 22:39
0

Any variable used inside a function is by default limited to the local function scope.

See: http://php.net/manual/en/language.variables.scope.php

You can do what you want if you make $newfile a global variable or pass it in as another function argument.

0

Function is out of scope. You could send the opened document direct to the function, that would allow it to be used within scope of the function.

function write($newfile,$text,$tab){
//the rest of your code
}

$returned = write($newfile,$text,$tab);
Leon
  • 26