Include statment have different behavior when called from a function?
<p>Im getting an strange result with this particular code. Let me explain my self in short way because is a long code. Basically the hole thing works like this:</p>

<p>In the beginning of my script I call my class <code>fg_membersite</code> and some constants:</p>

<pre><code>require_once("registration/include/membersite_config.php");
</code></pre>

<p>After that, I check some POST variables, if the code detects something wrong immediately calls the function <code>the_error()</code> (located on the same script). If there is no error, there is no call to the function and the execution goes.</p>

<p>An example of checking:</p>

<pre><code>$result=mysql_query("SELECT id FROM ".table_links." WHERE url = '".$fgmembersite-&gt;SanitizeForSQL($_POST['j_title'])."';");
if (mysql_num_rows($result)&gt;0) {
the_error('The url '. siteurl .'/'. $_POST['j_title'].' is currently in use, please use another one.');
}
</code></pre>

<p>After a series of POST and SQL checks there is the final part when I save to the database and print the result message:</p>

<pre><code>$fgmembersite-&gt;DBLogin();
mysql_query("INSERT INTO " . table_links . " (id, usr_code, visits, url, datetime, type, description, secu_type, password, phone, address, email, photo, id_number) VALUES (NULL, '".$_SESSION['user_code']."', '0', '".$fgmembersite-&gt;SanitizeForSQL($_POST['j_title'])."', NOW(), '0', '".$fgmembersite-&gt;SanitizeForSQL($_POST['j_desc'])."', '".$secutype."', '".$mod_pass."', '".$phone_check."', '".$address_check."', '".$email_check."', '".$photo_check."', '".$id_check."');");
echo '&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;';
include("parts/head.php");
echo '&lt;/head&gt;
&lt;body&gt;
&lt;div id="footerfix"&gt;';
include("parts/header.php");
echo '&lt;div class="container"&gt;
      &lt;center&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;img src="css/img/ok.png"/&gt;&lt;/center&gt;
      &lt;div class="hero-unit"&gt;
        &lt;center&gt;
        &lt;h2&gt;Your track URL &lt;a href="'. siteurl . '/' . $_POST['j_title'].'"&gt;'.siteurl . '/' . $_POST['j_title'].'&lt;/a&gt; have been created.&lt;/h2&gt;';
        if ($secutype==0){echo '&lt;p&gt;This url is public. Click &lt;a href="profile.php"&gt;here&lt;/a&gt; to return to your dashboard.&lt;/p&gt;';}
        if ($secutype==1){echo '&lt;p&gt;This is a password protected url. Click &lt;a href="profile.php"&gt;here&lt;/a&gt; to return to your dashboard.&lt;/p&gt;';}
        if ($secutype==2){echo '&lt;p style="font-size:16px"&gt;The visitors of this URL will need an special QR code. We offer you two options:&lt;/p&gt;
        &lt;p&gt;1. Download the &lt;a href="http://chart.googleapis.com/chart?chs=70x70&amp;cht=qr&amp;chf=bg,s,FFFFFF00&amp;chl='.$mod_pass.'"&gt;QR code image&lt;/a&gt; and add it to your personal design &lt;br&gt;2. Or Download a ready to print business card model&lt;/p&gt;&lt;br&gt;&lt;p&gt;Click &lt;a href="profile.php"&gt;here&lt;/a&gt; to return to your dashboard.&lt;/p&gt;';}
        echo '&lt;/center&gt;
      &lt;/div&gt;
    &lt;/div&gt;';
    include("parts/footer.php");
    echo '
    &lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;';
}
</code></pre>

<p>Take in consideration that all the included files, like <code>head.php</code> and <code>header.php</code> need the <code>fg_membersite</code> loaded before you include them. Until now the code works fine, but if the function <code>the_error</code> is called, I get several errors telling me that the <code>head.php</code> and <code>header.php</code> don't find the class loaded. This is <code>the_error</code>:</p>

<pre><code>function the_error($error) {
    echo '&lt;!DOCTYPE html&gt;
          &lt;html lang="en"&gt;
          &lt;head&gt;';
    include("parts/head.php");
    echo '&lt;/head&gt;
          &lt;body&gt;
          &lt;div id="footerfix"&gt;';
    include("parts/header.php");
    echo '&lt;div class="container"&gt;&lt;center&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;img src="css/img/notok.png"/&gt;&lt;/center&gt;&lt;div class="hero-unit"&gt;&lt;center&gt;&lt;h2&gt;Error&lt;/h2&gt;';
    echo '&lt;p&gt;'.$error.'&lt;/p&gt;';
    echo '&lt;/center&gt;&lt;/div&gt;&lt;/div&gt;';
    include("parts/footer.php");
    echo '&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;';
    exit;
}
</code></pre>

<p>As you can see, <code>the_error()</code> is almost the same code I use  to print the results. So, why the include works fine when I call them outside the function? Seems like when I use include in the PHP script outside a function it just copy the code from the external file into the original script (which is what I need) and when I use the include inside a function it first runs the remote script and then add the result (which will not work because the class is loaded on the script who use the include).</p>

<p>If I'm right and knowing the code flow, how do you suggest to solve this issue?</p>
