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

Ask A Question

Stats

  • Questions 513k
  • Answers 513k
  • 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 You're very close. Concatenating the arrays is done with the… May 16, 2026 at 5:47 pm
  • Editorial Team
    Editorial Team added an answer I've figured out a way to pull in multiple branches… May 16, 2026 at 5:47 pm
  • Editorial Team
    Editorial Team added an answer Checking for different ls output would send a message even… May 16, 2026 at 5:47 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 want to have a class with a private static data member: class C
why isn't it possible to define another key than @PrimaryKey @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) private
I'm not sure if this is even possible, but here goes: I have a
In a class, I've got a trace method that looks like this: [System.Diagnostics.ConditionalAttribute(Trace)] private
I have this class public class Item { public Coordinate coordinate { get; set;
Is it possible to persist a class in session that I define in VBScript
Can anybody tell me why is this not working. I have created a WCF
Is it possible to define a large portion, if not the entire, web.config of
Is it possible to define a dependency in the pom such that it has
I've had a good read of the PHP specs on overloading , and most

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.