简体   繁体   中英

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?

Maybe (ie, 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM