title: PHP : Make other functions access the $conn variable inside my database connection function
body: <p><strong>Make other functions access the $conn variable inside my database connection function</strong></p>
  
  <p>So here I am absolutely desperate trying to make something work. I know what im trying to do is not OOP neither 100% best practice. It is not for a live website, I am just learning some basic PHP concepts on XAMPP.</p>
  
  <p><strong>What I am trying to do</strong> is to make the $conn variable inside my database connection function accessible to all other functions that need it. I am thinking of passing it as a parameter, <strong>but how can this be done</strong>? I prefer not using PHP's "global" or $GLOBALS. My method of working right now is with mysqli using procedural methods.</p>
  
  <p>For example I have something like this:</p>
  
  <pre><code>function db () {
  $conn = mysqli_connect ("localhost", "root", "", "database");
  }
  
  function someFunction () {
  $result = mysqli_query ($conn, "SELECT * FROM examples)
  }
  </code></pre>
  
  <p>I never found the answer to my issue...most solutions which I recently got familiar with are OOP based or use somewhat questionable methods.</p>
  
  <p><em>------------------------------------------------------------------------------------------------------------------------------------</em></p>
  
  <p><strong>SOLUTION A - I would rather avoid not having my connection in a wrapper and using global variables:</strong></p>
  
  <pre><code>global $conn = mysqli_connect ("localhost", "root", "", "database");
  global $conn;
  
  function someFunction () {
  global $conn;
  $result = mysqli_query ($conn, "SELECT * FROM examples)
  }
  </code></pre>
  
  <p><strong>SOLUTION B - I am not ready for OOP yet but I know this works. The point is I want to learn something different:</strong></p>
  
  <pre><code>class Database
  {
      private static $conn;
  
      public static function getObject()
      {
          if (!self::$conn)
              self::$conn = new mysqli("localhost", "root", "", "database");
  
          return self::$conn;
      }
  }
  
  function someFunction () {
  $result = mysqli_query (Database::$conn, "SELECT * FROM examples)
  }
  </code></pre>
  
  <p><strong>SOLUTION C - Not using functions at all...just keeping it unwrapped which I dont find very practical in the long term:</strong></p>
  
  <pre><code>$conn = mysqli_connect ("localhost", "root", "", "database");
  
  $result = mysqli_query ($conn, "SELECT * FROM examples)
  </code></pre>
  
  <p><em>------------------------------------------------------------------------------------------------------------------------------------</em></p>
  
  <p><strong>THE SOLUTION I AM TRYING TO ACHIEVE:</strong></p>
  
  <pre><code>function db () {
  $conn = mysqli_connect ("localhost", "root", "", "database");
  return $conn;
  }
  
  function someFunction () {
  $conn = db ();
  $result = mysqli_query ($conn, "SELECT * FROM examples)
  }
  </code></pre>
  
  <p><strong>OR Something like this where I just pass in the connection as a parameter or something (pseudo code at the moment)</strong></p>
  
  <pre><code>function db () {
  $conn = mysqli_connect ("localhost", "root", "", "database");
  }
  
  function someFunction ($conn) {
  $result = mysqli_query ($conn, "SELECT * FROM examples)
  }
  </code></pre>
  
  <p><em>------------------------------------------------------------------------------------------------------------------------------------</em></p>
  
  <p><strong>So how do I achieve something like the last two but which actually works. Is this concept possible?</strong></p>