PHP: Weird function behavior depends on how I include the script it's contained in? I'm at a loss
<p>I honestly have little clue what facts are relevant to this issue, so I'll go from the beginning.</p>

<p>I have bits of text that I'd like to format Markdown-style urls in before plugging into a template, but I don't want them to be wrapped in &lt;p&gt;'s or any other block-level tags.</p>

<p>To do this, I worked my way through the <a href="http://michelf.com/projects/php-markdown/" rel="nofollow">PHP Markdown</a> code, isolated the stuff needed for the url formatting and threw the functions together into a file called 'MarkdownUrl.php' in a folder called 'functions' at my include path.</p>

<p>I'll post that code at the bottom.</p>

<p>For now, let's test it:</p>

<pre class="lang-php prettyprint-override"><code>&lt;?php

require_once 'functions/MarkdownUrl.php';

$text = "Hello, world. [This is a link.](http://www.google.com)";

echo MarkdownUrl($text);

?&gt;
</code></pre>

<p>Output:</p>

<blockquote>
  <p>Hello, world. <a href="http://www.google.com" rel="nofollow">This is a link.</a></p>
</blockquote>

<p>So far so good.</p>

<p>I also have a file called 'loader.php' at the include path. It contains a class autoloading function which it registers with <code>spl_autoload_register()</code> and another function called <code>loadFunc()</code> which will <code>require_once</code> a php file from the 'functions' directory according to the name it's passed. The latter is what matters here.</p>

<pre class="lang-php prettyprint-override"><code>&lt;?php

function loadFunc($funcName)
{
    if (!function_exists($funcName)) {
        require_once 'functions/' . $funcName . '.php';
    }

}

function loadClass($className)
{
    if (!class_exists($className, false)) {
        $classPath = str_replace('_', '/', $className);
        require_once 'classes/' . $classPath . '.php';
    }

}

spl_autoload_register('loadClass');

?&gt;
</code></pre>

<p>Ignoring the 'whys' and 'wherefores' of including code in this manner for the moment, the following <em>should</em> be effectively equivalent to the test code I posted earlier, right?</p>

<pre class="lang-php prettyprint-override"><code>&lt;?php

require_once 'loader.php';
loadFunc('MarkdownUrl');

$text = "Hello, world. [This is a link.](http://www.google.com)";

echo MarkdownUrl($text);

?&gt;
</code></pre>

<p>And, yet... output:</p>

<blockquote>
  <p>Hello, world. [This is a link.](http://www.google.com)</p>
</blockquote>

<p>No errors are issued on running the above.</p>

<p>And, I have confirmed that the function <code>MarkdownUrl()</code> <em>is</em> being called (at least, a test <code>echo</code> I inserted at the top of the function fired).</p>

<p>Yet, the output is unformatted. WTF?</p>

<p>A couple I'm-not-sure-if-these-matter-at-all details: shared web host, include path specified via php.ini file in directory the test script runs in.</p>

<hr>

<p>Contents of MarkdownUrl.php:</p>

<pre class="lang-php prettyprint-override"><code>&lt;?php

$nested_brackets_depth = 6;
$nested_url_parenthesis_depth = 4;
$no_entities = false;

function doAnchors_inline_callback($matches) {
    $whole_match    =  $matches[1];
    $link_text      =  $matches[2];
    $url            =  $matches[3] == '' ? $matches[4] : $matches[3];
    $title          =&amp; $matches[7];

    $url = encodeAttribute($url);

    $result = "&lt;a href=\"$url\"";
    if (isset($title)) {
        $title = encodeAttribute($title);
        $result .=  " title=\"$title\"";
    }

    $result .= "&gt;$link_text&lt;/a&gt;";

    return $result;
}

function encodeAttribute($text) {
#
# Encode text for a double-quoted HTML attribute. This function
# is *not* suitable for attributes enclosed in single quotes.
#
    $text = encodeAmpsAndAngles($text);
    $text = str_replace('"', '&amp;quot;', $text);
    return $text;
}

function encodeAmpsAndAngles($text) {

    global $no_entities;

#
# Smart processing for ampersands and angle brackets that need to 
# be encoded. Valid character entities are left alone unless the
# no-entities mode is set.
#
    if ($no_entities) {
        $text = str_replace('&amp;', '&amp;amp;', $text);
    } else {
        # Ampersand-encoding based entirely on Nat Irons's Amputator
        # MT plugin: &lt;http://bumppo.net/projects/amputator/&gt;
        $text = preg_replace('/&amp;(?!#?[xX]?(?:[0-9a-fA-F]+|\w+);)/', 
                            '&amp;amp;', $text);;
    }
    # Encode remaining &lt;'s
    $text = str_replace('&lt;', '&amp;lt;', $text);

    return $text;
}

function MarkdownUrl($text) {

    global $nested_brackets_depth, $nested_url_parenthesis_depth;

    $nested_brackets_re = 
            str_repeat('(?&gt;[^\[\]]+|\[', $nested_brackets_depth).
            str_repeat('\])*', $nested_brackets_depth);

    $nested_url_parenthesis_re = 
            str_repeat('(?&gt;[^()\s]+|\(', $nested_url_parenthesis_depth).
            str_repeat('(?&gt;\)))*', $nested_url_parenthesis_depth);

    $text = preg_replace_callback('{
        (               # wrap whole match in $1
          \[
            ('.$nested_brackets_re.')   # link text = $2
          \]
          \(            # literal paren
            [ \n]*
            (?:
                &lt;(.+?)&gt; # href = $3
            |
                ('.$nested_url_parenthesis_re.')    # href = $4
            )
            [ \n]*
            (           # $5
              ([\'"])   # quote char = $6
              (.*?)     # Title = $7
              \6        # matching quote
              [ \n]*    # ignore any spaces/tabs between closing quote and )
            )?          # title is optional
          \)
        )
        }xs','doAnchors_inline_callback', $text);

    return $text;
}

?&gt;
</code></pre>
