title: Trying to understand the logic behind the PHP parser / functions
body: <p>Say I have this <code>b.php</code> file</p>
  <pre><code>&lt;?php 
     function ff() { echo &quot;B&quot;; }
  </code></pre>
  <p>and this <code>a.php</code> file</p>
  <pre><code>&lt;?php 
  
  if ( true ) {
    include (&quot;b.php&quot;);
    ff();
    exit;
  }
  
  function ff() { echo &quot;A&quot;; }
  </code></pre>
  <p>Running <code>php a.php</code> gives</p>
  <pre><code>PHP Fatal error:  Cannot redeclare ff()
  </code></pre>
  <p>Hmm, ok. So what if I re-run <code>php a.php</code> with this <code>a.php</code> code</p>
  <pre><code>&lt;?php 
  
  if ( true ) {
    include (&quot;b.php&quot;);
    ff();
    exit;
  }
  else {
    function ff() { echo &quot;A&quot;; }
  }
  </code></pre>
  <p>This time that works, and I get <code>B</code> as expected.</p>
  <p>In the first case, PHP reads and parse the whole file. Meaning, during <em>compilation</em> it sees the 2 same functions declarations, though the <code>exit</code> guarantees that at runtime we won't get up to the 2nd <code>ff()</code>.</p>
  <p>However in the 2nd case, it sees it at <em>run</em> time, but the compiler is not upset about seeing this function declared twice...</p>
  <p>Seems the compiler behaves differently based on if the function is top-level or not... while</p>
  <blockquote>
  <p>All functions and classes in PHP have the global scope</p>
  </blockquote>
  <p>(the <a href="https://www.php.net/manual/en/functions.user-defined.php" rel="nofollow noreferrer">doc</a>)</p>
  <p>Now, how to turn that into a non opinion-based question :)<br>
  So it seems the options are either to rename <code>ff</code> to something like <code>ffa</code> and <code>ffb</code>, or add a long <code>else { ... }</code>. or change the whole structure...</p>
  <p>Is this how PHP works, or that can be changed in the settings?</p>