michael
michael

Reputation: 406

include and require functions in PHP are not adding contents but not erroring out

I'mu using the Codeigniter framework and I have an include_once('filename'); at the beginning of a function. In stepping through the code in debug mode the caller function evaluates the filename properly. I can then actually steps through the file line by line I'm attempting to include. The variables show up as I'm in the included file properly, and as soon as I exit out of the include file and back to the caller function that has the include_once() call, all the data is gone and the variables that were set in 'filename' are uninitialized. What could possibly be causing this type of behavior?

Upvotes: 0

Views: 84

Answers (1)

Damien Pirsy
Damien Pirsy

Reputation: 25435

Maybe (i.e., surely) the problem is that, as you said, you're including the file INSIDE A FUNCTION. So, all variables will exist only inside the scope of that function, and not outside of it.

You could either declare those variables global (which is frowned upon), or better have the function return the values you need from the variables of your included file.

Learn more on PHP Variable scopes

Upvotes: 1

Related Questions