Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7411663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:25:19+00:00 2026-05-29T06:25:19+00:00

Is there a way to include a file outside of a function and have

  • 0

Is there a way to include a file outside of a function and have it work?

ie. I have a file db.inc.php, this contains my db connection string:

switch (DB_TYPE) {
case MYSQL :
    try {
        $DBH = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME . "", DB_USER, DB_PASS);
        $DBH -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch(PDOException $e) {
        echo "Error in Connection" . $e -> getMessage();
    }
}

Now I have require this file my in my functions file, functions.php:

require('db.inc.php')
function add() {

        $n = $DBH -> prepare("");
        $n -> execute((array)$s);
}

so the file is added in properly, but my function cannot access the $DBH handle. Only when I include the file in my function:

function add(){
require('db.inc.php')

....etc....

}

will my function work.

As I am going to have at least 4-5 functions in many files, any possibility of just requiring it outside the function and get the function to work?

Thank and regards

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:25:20+00:00Added an answer on May 29, 2026 at 6:25 am

    After including the file outside the function, you could declare the variable as global, but that’s really a bad way to do things. Instead, you should have the caller of the function pass handle as an argument. Failing that, you could create a factory for the database and call it within each function.

    Why do you have to call the require outside the function?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to include php file using require and calling a php
Is there a way to include an entire text file as a string in
I have a file that require()'s a namespace, as such: <?php require_once('Beer.php'); // This
This question should be pretty simple. I have a php file in a directory
Is there a way to include a File Content inside the Class ,not just
Is there a way to include a javascript file only once or declare a
Is there a way to include a javascript file to a XBL instead of
I have an include file menu.inc that I would like to store in the
Is there an easy way to include file upload capabilities to the admin interface
I'd like to know if there is a way to include a file in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.