title: How do I call a function included in another file? PHP
body: <p>I couldn't  find an answer to this question elsewhere, I was hoping someone here might know. I've spent 9 hours straight trying to get this to work, I've been searching non-stop.</p>
  
  <p>I have three PHP files. The third file contains my PHP functions to check for existing users and add users to the database </p>
  
  <p>user.inc.php</p>
  
  <pre><code>&lt;?php
  //checks if given username exists in database
  
  function user_exists($user){
      $user = mysqli_real_escape_string($user);
      $sql= "SELECT COUNT(`user_id`) FROM `users` WHERE `user_name` = '{$user}'";
      $result = mysqli_query($con,$sql);
  
      if ($result == 1){
      return true;
      }
      else {
      return false;   
      }
  
  }
  
  function email_exists($email){
      $user = mysqli_real_escape_string($email);
      $sql="SELECT COUNT(`user_id`) FROM `users` WHERE `user_email` = '{$email}'";
      $result = mysqli_query($con,$sql);
  
  //  $row = mysqli_fetch_assoc($total);
      if ($result == 1){
      return true;
      }
      else {
      return false;   
      }
  }
  
  
  //checks if given username/password is valid
  function valid_credentials($user,$pass){
      $user = mysqli_real_escape_string(htmlentities($user));
      $pass = sha1($pass);
      $sql= "SELECT COUNT(`user_id`) FROM `users` WHERE `user_name` = '{$user}'AND `user_password` ='{$pass}'";
      $result = mysqli_query($con,$sql);
  
  if ($result == 1){
      return true;
      }
      else {
      return false;   
      }
  }
  
  
  //adds a user to the database
  function add_user($user,$pass,$email){
      mysqli_query($con,"INSERT INTO`users` (`user_name`,`user_password`,`user_email`) VALUES ('a','b','c')");
      $user = mysqli_real_escape_string(htmlentities($user));
      $pass = sha1($_REQUEST[$pass]); 
      mysqli_query($con,"INSERT INTO`users` (`user_name`,`user_password`,`user_email`) VALUES ('{$user}','{$pass}','{email}')");
  }
  
  
  
  ?&gt;
  </code></pre>
  
  <p>The second PHP file starts the connection to the database and includes the previous file at the bottom.</p>
  
  <p>init.inc.php</p>
  
  <pre><code>&lt;?php 
  
  session_start();
  
  $exceptions = array('registerPage','login');
  
  $page = substr(end(explode('/',$_SERVER['SCRIPT_NAME'])), 0, -4);
  
  if (in_array($page, $exceptions)=== false){
      if (isset($_SESSION['username'])=== false ){
          header('Location: login.php');
          die();
      }
  }
  
  $con = mysqli_connect('127.0.0.1','root','','yingyujiaocheng');
  if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
  mysqli_query($con,"INSERT INTO `users` (`user_name`,`user_password`) VALUES ('user','pass')");
  
  $path = dirname(__FILE__);
  
  include("{$path}/inc/user.inc.php");
  ?&gt;
  </code></pre>
  
  <p>The third PHP file includes the second file at the top and also has HTML and forms. When the form on the 3rd page is filled out and submitted, it sends the information to the top of the 3rd page to the PHP script. This is processed for errors which calls functions from the first files. The database works, I have done MYSQLI commands from every place, the only thing not working is the call from the 3rd PHP file to the first PHP file. The functions user_exists, email_exists and add_user to not call correctly.</p>
  
  <p>Here is the third file:</p>
  
  <p>registerPage.php</p>
  
  <pre><code> &lt;?php error_reporting(E_ALL);  
  
    include('core/init.inc.php');
  
    $errors = array();
  
    if(isset($_POST['username'],$_POST['password'],$_POST['email'])){
      $username = $_POST['username'];
      $password = $_POST['password'];
      $email = $_POST['email'];
  
       if (empty($username)){ 
           $errors[] = 'The username cannot be empty.';
       }
  
       if (empty($password)){
           $errors[] = 'The password cannot be empty.';    
       }
  
       if (empty($email)){
           $errors[] = 'The email field cannot be empty.';     
       }  
  
       if (user_exists($username)){       
           $errors[] = 'The username is already taken';       
       }
  
       if (email_exists($email)){
           $errors[] = 'The email already taken';         
       }
  
  
  
      if (empty($errors)){    
  
           add_user($username,$password,$email);
          //$_SESSION['username'] = htmlentities($username);
  
          //header('Location: protected.php');
          // die();
       }
  
    }
  
    ?&gt;
  
    &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
    &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
    &lt;head&gt;
    &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
    &lt;title&gt;Untitled Document&lt;/title&gt;
  
    &lt;link href="ext/Styles/styleSheet.css" rel="stylesheet" type="text/css" /&gt;
    &lt;/head&gt;
  
    &lt;body&gt;
    &lt;div class="header" align="centre"&gt;
  
    &lt;img src="ext/Images/Logo.png" width="150" height="80" style="float:left;" /&gt;
    &lt;h2 class="headerFontClickedSmall" style="float: right; margin-top:36px; margin-right: 60px"&gt;  &lt;/h2&gt;
    &lt;h2 class="headerFontUnclickedSmall" style="float: right; margin-top:36px; margin-right: 10px"&gt; / &lt;/h2&gt;
    &lt;h2 class="headerFontUnclickedSmall" style="float: right; margin-top:36px; margin-right: 10px"&gt;  &lt;/h2&gt;
    &lt;h2 class="cornerBox1" style="float: right; margin-top:20px; margin-right: 50px"&gt;  &lt;/h2&gt;
  
    &lt;/div&gt;
  
    &lt;div class="content"&gt;
  
    &lt;br /&gt;
    &lt;br /&gt;
    &lt;br /&gt;
    &lt;br /&gt;
    &lt;br /&gt;
    &lt;br /&gt;
    &lt;br /&gt;
    &lt;?php echo "Username is :" . $username . "&lt;br&gt;";
  echo "Password is :" . $password;
  ?&gt;
    &lt;br /&gt;
    &lt;h1 class="contentHeader" style=""&gt;  &lt;/h1&gt; 
  
    &lt;div&gt;
        &lt;?php 
            if (empty($errors) === false){
    ?&gt;
    &lt;ul&gt;
        &lt;?php
  
            foreach ($errors as $error){
              echo "&lt;li&gt;{$error}&lt;/li&gt;";
           }
            ?&gt;
        &lt;/ul&gt;
           &lt;?php
           }
        ?&gt;
    &lt;/div&gt;
  
    &lt;form action="registerPage.php" method="POST"&gt;
    &lt;h1 class="contentRegisterText" style=""&gt; : &lt;input class="inputbox" style="margin-left:30px" type="text"  
                                                           name = "username" id="username"/&gt; &lt;/h1&gt;
    &lt;br /&gt;
  
    &lt;h1 class="contentRegisterText" style=""&gt; : &lt;input class="inputbox" style="margin-left:30px" type="text" 
                                                           name = "email" id="email"/&gt; &lt;/h1&gt;
    &lt;br /&gt;
  
    &lt;h1 class="contentRegisterText" style=""&gt;: &lt;input class="inputbox" style="margin-left:30px" type="password" 
                                                           name = "password" id="password"/&gt; &lt;/h1&gt;
  
    &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
  
    &lt;input style="margin-left:30px" type="submit"  value = "" id="Register"/&gt; &lt;/form&gt;
    &lt;/div&gt;
  
    &lt;div class="footer" align="center" &gt;
        &lt;div class="floating-box" style="margin-top:40px" &gt;
            &lt;dl&gt;
                &lt;dt&gt;&lt;h1 class="footerTitle"&gt;社交媒体&lt;/h1&gt;&lt;/dt&gt;
                &lt;br /&gt;
                &lt;dd&gt;&lt;a href="http://www.huya.com/lucio"&gt;
    &lt;img src="ext/Images/HuyaLogo.png" alt=" " width="42" height="42" outline="none"&gt;
    &lt;/a&gt; 
                    &lt;img src="ext/Images/weixinLogo.png" width="40" height="40" style="margin-left:3;"/&gt;&lt;img src="ext/Images/logo-qq.png" width="40" height="40" /&gt;&lt;/dd&gt;          
  
            &lt;/dl&gt;
        &lt;/div&gt;
  
        &lt;div class="floatingboxFooter1" style="margin-top:40px"&gt;
            &lt;dl&gt;      
                &lt;dt&gt;&lt;h1 class="footerTitle"&gt;&lt;/h1&gt; &lt;/dt&gt;
                &lt;br /&gt;
                &lt;dd&gt;&lt;h1 class="footerSmall"&gt;:&lt;/h1&gt;&lt;/dd&gt;
                &lt;dd&gt;&lt;h1 class="footerSmall"&gt;Weixin:  &lt;/h1&gt;&lt;/dd&gt;
                &lt;dd&gt;&lt;h1 class="footerSmall"&gt;QQ:      &lt;/h1&gt;&lt;/dd&gt;
            &lt;/dl&gt;
        &lt;/div&gt;
        &lt;div class="floating-box" style="margin-top:64px"&gt;
            &lt;dl&gt;      
                &lt;br /&gt;
                &lt;dd&gt;&lt;h1 class="footerSmall"&gt;bangzhu@yingyujiaocheng.com&lt;/h1&gt;&lt;/dd&gt;
                &lt;dd&gt;&lt;h1 class="footerSmall"&gt;yingyujiaocheng&lt;/h1&gt;&lt;/dd&gt;
                &lt;dd&gt;&lt;h1 class="footerSmall"&gt;yingyujiaocheng&lt;/h1&gt;&lt;/dd&gt;
            &lt;/dl&gt;
        &lt;/div&gt;
  
    &lt;div class="floatingboxFooter1" style="margin-top:40px"&gt;
            &lt;dl&gt;      
                &lt;dt&gt;&lt;h1 class="footerTitle"&gt;&lt;/h1&gt; &lt;/dt&gt;
                &lt;br /&gt;
                &lt;dd&gt;&lt;h1 class="footerSmall"&gt;&lt;/h1&gt;&lt;/dd&gt;
                &lt;dd&gt;&lt;h1 class="footerSmall"&gt;&lt;/h1&gt;&lt;/dd&gt;
                &lt;dd&gt;&lt;h1 class="footerSmall"&gt;&lt;/h1&gt;&lt;/dd&gt;
            &lt;/dl&gt;
  
  
  
    &lt;/div&gt; 
  
    &lt;/body&gt;
    &lt;/html&gt;
  </code></pre>
  
  <p>Thank you for looking, any help is appreciated,</p>
  
  <p>Cheers</p>
  
  <p>Lucio</p>