How to use an array from file A in file B
What is the right way to use an array from file A in file B?
When u use the example below, i get aan error (in Zend "Notice: Undefined variable: prefs") Then, if i var_dump $prefs, it shows me the correct array of type array.
Example:
FILE A
$prefs = array('test', 'anothertest');
FILE B
require_once('PATH_TO_FILE_A');
ec开发者_运维知识库ho $prefs[0];
//output: test
If you don't like this warning message, you can set a NULL $prefs before including file.
$prefs = '';
require_once('PATH_TO_FILE_A');
echo $prefs[0];
//output: test
Zend is warning you because actually $prefs isn't declared on file B .
P.S. : Really, you don't need this. Just ignore that warning message.
If you're asking if that's the right way to do that, then yes, it is. Your code should work as written.
When you require
or include
files into a PHP document, PHP treats them as if they were literally part of the master document. In your case, imagine copy and pasting the contents of File A into the top of File B. That's how PHP will treat it.
精彩评论