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

Related Questions

I have one line in php file like this: <a class=hide-if-no-js onclick=findPosts.open( 'media[]','<?php echo
I have a functions.php file that has one PDO Connection i use to access
i have one php file which process adding of record in Database fro array.
Iam making an app which has one index.php file. I also have these other
I have a PHP file with one simple echo function: echo 'アクセスは撥ねりません。'; but when
I have a php file setup to pull through ONE XML data feed, What
I have problem with session in cakephp.I have one file chat.php that is in
using CodeIgniter normally one has to specify the controllers in the config/routes.php file. This
I have made a code -- <?php header(Content-type: text/javascript); require_once('../php/config.php'); $domail_theme = $_GET['theme']; ?>
I have this php script in an external file trying to get random question

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.