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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:32:58+00:00 2026-05-13T05:32:58+00:00

In one PHP file, I have this code: require_once $_SERVER[‘DOCUMENT_ROOT’] . ‘/custom/functions.php’; global $testVar;

  • 0

In one PHP file, I have this code:

require_once $_SERVER['DOCUMENT_ROOT'] . '/custom/functions.php';
global $testVar;
var_dump($testVar);

In the functions.php file, I have this at the beginning, followed by a few other functions:

function pr($s) {
    echo '<pre>', htmlspecialchars(print_r($s,true)), '</pre>';
}

$testVar = 'hello world';

When running the first file, the variable comes back as NULL. I added the global bit but it shouldn’t be necessary. This is part of a Joomla module but I’ve never had problems including files before, it should just work like regular PHP. Why might this be happening?

  • 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-13T05:32:58+00:00Added an answer on May 13, 2026 at 5:32 am

    First, try to use Joomla’s path constants such as JPATH_BASE instead of $_SERVER['DOCUMENT_ROOT']. Joomla has a lot of useful constants, check it’s documentation.

    I’ve read your answer, and reading php documentation I tried to find a reason to why you need to use global keyword twice.

    First, Variable scope.

    The scope of a variable is the context within which it is defined. For the most 
    part all PHP variables only have a single scope.
    
    (...)
    
    However, within user-defined functions a local function scope is introduced. 
    Any variable used inside a function is by default limited to the local 
    function scope. 
    

    The variable isn’t in a function scope, so that’s why we thought the NULL was a strange behavior.

    But then I read include and found something interesting:

    (...)
    Any variables available at that line in the calling file will be available 
    within the called file, from that point forward. However, all **functions** 
    and **classes** defined in the included file have the global scope. 
    

    I can’t see any mention about the variables being global in this paragraph. So,it seens that, being cumbersome or not, your solution is the right thing to do when you want to use global variables like that.

    In your situation, if doing this is cumbersome, I would create a simple class. If you have just helper functions in your file, create a class Util{} with a lot of methods and $testVar as an attribute.

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

Sidebar

Ask A Question

Stats

  • Questions 512k
  • Answers 512k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try the surround.vim plugin. I find it an essential addition… May 16, 2026 at 5:37 pm
  • Editorial Team
    Editorial Team added an answer It is impossible to spoof a TCP connection over the… May 16, 2026 at 5:37 pm
  • Editorial Team
    Editorial Team added an answer Center your control in the designer and set its Anchor… May 16, 2026 at 5:37 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a PHP website I'm maintaining and I've confirmed that this worked at
Hey everyone... I'm not too familiar with PHP... but I have PHP code that
I have 2 servers that are on completely separate OS' and configurations. One properly
I've defined an abstract superclass in one file and a subclass in another. I
I have a header.html which start with session_start();. Then the following code, $_SESSION['cart'][$sw_id] is
I want to put my html navigation in a separate php file so when
I've created a php file called pagebase.php that I'm quite proud of. It contains
Site . This code is not optimized nor is it the best method. If
I'm stumped! In PHP in Netbeans (6.8), a project has two files, file1.php and
Please I am very new to PDO, rather a novice at PHP as well.

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.