How to use variables from one PHP file in another file's function?
<p>NOTE: I have read about 25 questions thus far that are peripherally related to this, but never answer the question directly of HOW TO USE THOSE VARIABLES CORRECTLY. I apologize if it sounds like a duplicate.</p>

<p>I am rewriting a flat e-commerce site (all hand-made<code>.htm</code> files!) so that some of the pages are generated using PHP scripts. This is all just to make my life easier for now. I have told my boss repeatedly that this site needs to be nuked from orbit, and there is a full-blown CMS in the pipeline. In the meantime this site NEEDS to be revamped. Site in question: <a href="http://www.leather.com" rel="nofollow">http://www.leather.com</a></p>

<p>I have been using a <code>constructor_page.php</code> that declares and initializes a few variables such as <code>$itemName</code> and <code>$imagePath</code> that are used in <code>include()</code>'d content pages (trimmed example follows):</p>

<pre><code>&lt;?php
$imagePath = 'http://leather.com/images/harley_leather_boots/d81024_ladies_harley_leather_boots_360.jpg';
$itemName = 'Faded Glory Harley Boots';
include 'library/content.php'; //this in turn includes more files like the nav bar
?&gt;
</code></pre>

<p>This way I just save <code>constructor_page.php</code> as the item page in question, modify a few variables, and the <code>include()</code> statements take care of the rest. This part works okay. </p>

<p>My issue is with generating "department" pages automatically from the files in the same directory. I would like the index.php to grab those same variables in a loop:</p>

<pre><code>&lt;?php
foreach (glob("*.php") as $filename) {
include $filename;
echo '&lt;div class="related"&gt;';
echo '&lt;a href="';
echo $filename; 
echo '"&gt;';
echo '&lt;img src="';
echo $imagePath;
echo '" alt="';
echo $imageAlternateText;
echo '" class="frame-related align-left-related"';
echo '&gt;&lt;/a&gt;';
echo '&lt;a href="';
echo $filename; 
echo '"&gt;';
echo $itemName; 
echo '&lt;/a&gt;';
echo '&lt;/div&gt;';
}
?&gt;
</code></pre>

<p>This is intended to, for each php file (yes I know it'll recurse to itself), grab the relevant variables and create a nice picture/link inside a  that floats left, in essence generating the department page thumbnails automatically based on whatever php files it finds in there. </p>

<p>Yes, it's horrible. Yes, it's hackish. Needless to say, it does not work due to the scope of the variables. I am open to suggestion here, as all I need is for the department page to generate little pictures and links based on what it finds in the folder SOME HOW. Thanks in advance!</p>
