Make function variable available to other php files
<p>I have a paging.php file, which has a function <code>latest($imagesPerPage, $site) {</code>.</p>

<p>Inside that function, I have a variable $lastPage:</p>

<pre><code>$catResult-&gt;data_seek(0); 
$totalComics = 0;
while ($row = $catResult-&gt;fetch_assoc()) {
    $totalComics++;
}

global $lastPage;
$lastPage = ceil($totalComics/$imagesPerPage);
</code></pre>

<p>I have another file, homepage.php, which needs to use <code>$lastPage</code> defined above in the paging.php file's "latest()" function.</p>

<hr>

<p>At the top of homepage.php, I include the paging file: <code>include 'scripts/paging.php';</code></p>

<p>I then call <code>&lt;?php echo latest(15, $site); ?&gt;</code> to display some images...</p>

<p>And below, I want to handle page numbers and navigation, and need to use the $lastPage variable:</p>

<pre><code>    for($i = 1; $i &lt;= $lastPage; $i++) {
        echo '&lt;li&gt;&lt;span class=navItems&gt;&lt;a href="?site=' . $site . '&amp;cat=' . $cat . '&amp;page=' . $i .'"&gt;' . $i . '&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;';
    }
</code></pre>

<p>homepage.php keeps complaining that $lastPage is undefined... I've tried <code>global $lastPage</code>, <code>$GLOBALS[$lastPage]</code>... but it's still not available.</p>

<hr>

<p>My questions are:</p>

<ol>
<li><p>How can I make <code>$lastPage</code> available outside the function to the homepage.php file?</p></li>
<li><p>How can I make <code>$lastPage</code> available to other functions within paging.php?</p></li>
</ol>
