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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:04:35+00:00 2026-06-05T22:04:35+00:00

I have a config.php file that creates an array, something like $config = array(

  • 0

I have a config.php file that creates an array, something like

$config = array(
       'foo' => 'bar'
);
function foo()
{
       echo 'good';
}

I also have another utility.php file that prints stuff, that depends on config.php

require_once(-the absolute path to config.php-);
class Utility{
  function bar()
  {
    echo count($config);
    echo foo();
  }
}

I am in a situation where my index.php script depends on config.php as well as utility.php.
Therefore, when I include foo.php I am including config.php again. Something like

require_once(-the absolute path to config.php-);
require_once(-the absolute path to utility.php-);
echo count($config);
utility::bar();

This function prints out

1good

However, when I attempt to call Utility::bar, it prints 0 for count($config) – the $config array never gets created in utility.php, in spite of count($config) returning 1 in index.php. Interestingly, calling function foo() in utility.php still returns “good”.
Making $config global didn’t change anything (and I hear is bad style).

  • 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-05T22:04:37+00:00Added an answer on June 5, 2026 at 10:04 pm

    It looks like you have a variable scope issue. Read about PHP variable scope. As an example I think if you changed

    echo count($config);
    

    to

    global $config;
    echo count($config);
    

    it would work.

    • 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 that looks like this: <?php include(config.php); // put the
I have created a config file that looks like: $conf['db_hostname'] = localhost; $conf['db_username'] =
I am using php and mysql. I have a Database config file (db-config.php) which
I have a php script, test.php that has the contents <?php require_once(classes/user.php); echo test;
I have following in my config.php file for one of my web app. //db
I have a file that pulls some information from the database and creates some
I'm building a simple php app using OOP that creates a CSV file based
I have config.php with this... $dbhost = localhost; I want to be able to
in my config.php where i have all constants i set the PATH to a
I have se the below date format in the codeignitor config.php $config['log_date_format'] = 'd/m/Y';

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.