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 8964387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:34:36+00:00 2026-06-15T16:34:36+00:00

I have a file named config.php which contains a variable named $unit_names . In

  • 0

I have a file named “config.php” which contains a variable named $unit_names. In a class I have the following method which returns unit name by it’s id. I use this method for all rows in a table, but the problem is config.php is included only in the first call to the method. next calls cause the notice: undefined variable $unit_names...

If I replace include_once by include the notice disappears. But why? include_once should load the config.php file.

public function get_unit_name($unit_id) 
    {   
            include_once("config.php");
            return $unit_names[$unit_id];
    }   
  • 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-06-15T16:34:37+00:00Added an answer on June 15, 2026 at 4:34 pm

    include_once will not load the specified file at least once, it will load it exactly once! If you call the function the second time, the file has already been included and will therefore not be included again, hence the error message.

    The idea with this is that you use include_once / require_once when you include files that contain function or class definitions, i.e., files that would produce an error if they were loaded more than once. If it’s okay to load a file twice or more times, always use include() instead of include_once().

    Consider something like this instead (to save the cost of re-including the file every time):

    private $unit_names = null;
    
    public function get_unit_name($unit_id) {   
        if ($this->unit_names === null) {
            include("config.php");
            $this->unit_names = $unit_names;
        }
    
        return $this->unit_names[$unit_id];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a php file named config.php in which I defined the application settings
I have a config.php file for one of my site which holds information needed
I have a config file that is a php array named config.php. return array(
Hi I need to pass a variable called $process_id to the file config/update.php which
I have a database class in which I am calling a config file using
I have a file named configuration.xml which resides in classes folder of my WEB-INF
I have a txt file named 'a.txt' with the following content: Hi=Python Now, I
I have a Yii project with a main.php config file and dev.php config file
Possible Duplicate: Reading and Writing Configuration Files Okay, I have a config file, which
I have a file named my file.pdf and I can't delete this file with

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.