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

  • Home
  • SEARCH
  • 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 714373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:04:04+00:00 2026-05-14T05:04:04+00:00

Is it possible to define private variables in a PHP script so these variables

  • 0

Is it possible to define private variables in a PHP script so these variables are only visible in this single PHP script and nowhere else? I want to have an include file which does something without polluting the global namespace. It must work with PHP 5.2 so PHP namespaces are not an option. And no OOP is used here so I’m not searching for private class members. I’m searching for “somewhat-global” variables which are global in the current script but nowhere else.

In C I could do it with the static keyword but is there something similar in PHP?

Here is a short example of a “common.php” script:

$dir = dirname(__FILE__);
set_include_path($dir . PATH_SEPARATOR . get_include_path());
// Do more stuff with the $dir variable

When I include this file in some script then the $dir variable is visible in all other scripts as well and I don’t want that. So how can I prevent this?

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

    There are a few things you could do to keep $dir out of subsequent files

    Example 1

    set_include_path(dirname(__FILE__) . PATH_SEPARATOR . get_include_path());
    

    This is the most obvious.

    Example 2

    $dir = dirname(__FILE__);
    set_include_path($dir . PATH_SEPARATOR . get_include_path());
    // work with $dir
    unset($dir);
    

    Just unset the variable after defining it and using it. Note this will unset any variable named $dir used prior to including this script.

    Example 3

    define('DIR_THIS', dirname(__FILE__));
    
    set_include_path(DIR_THIS . PATH_SEPARATOR . get_include_path());
    

    It is less likely I suppose to redefine a global constant like this.

    Example 4

    function my_set_include_path {
      $dir = dirname(__FILE__);
      set_include_path($dir . PATH_SEPARATOR . get_include_path());
      // Do more stuff with the $dir variable
      $my_other_var = 'is trapped within this function';
    }
    
    my_set_include_path();
    

    You can define as many variables within that function and not affect the global namespace.

    Conclusion

    The first method is the easiest way to solve this problem, however because you want to use $dir again, it may not be ideal. The last example will at least keep that $dir (and any others defined in that function) out of the global namespace.

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

Sidebar

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.