views:

41

answers:

2

Hi. I have a long function with many if statements and such. The first thing i do in my function is this: $text = file($read_text_file);

The $text array contain all text in the site therefor I need this array to be available in my entire function and I though I would accomplice this by defining the variable first thing in the function, but now i discovered that later in a if statement, its blank. Why is this?

A: 

As William said in the comments above we really need to see some context of this problem in order to answer this question. There could be any number of issues happening. Are you sure that the file is being read correctly as well?

spinon
This probably should be a comment
John Conde
I agree. But at the time I didn't have the reputation score required to do so (50 - http://stackoverflow.com/faq) Though I do now. Sorry for the confusion.
spinon
I am sure the file is being read, but the function has 600 lines of code. I can't post all of it. Can't you guys just explain why this might happen? Isn't the variable global if you set it first in the function?
ganjan
If you are defining a variable inside a method and trying to use within that method only then there should be no reason why this isn't working. If you are defining a variable in one method and trying to use in another then obviously that is a problem. Without seeing more context it is impossible to guess how you are defining and using a variable.
spinon
A: 

Put var_dump($text); on the line immediately after $text = file($read_text_file);. If the array is populated, then you will get the values of the array. If not, then my guess is that there is a problem with the file itself.

Joseph
The file is fine. The array works in most of the code, but in some if statements it start to show up blank. Don't know why. I will try to isolate the problem, but its not so easy. Should I just define the variable as global from the start maybe?
ganjan
Ahh. So you are using it within functions then? Is it only in the functions that the variable is empty? If that is the case, you either need to pass the array as an argument in the function or declare the array to be global at the beginning of the function.
Joseph
Using global is not a particularly good idea; and certainly isn't the correct solution to this problem. Do you have anything in your code that updates $text once it has been set? When you discover it's blank, is it an empty array or an empty string?... var_dump($text) will tell you this
Mark Baker