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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:25:58+00:00 2026-05-31T06:25:58+00:00

I was trying to pass a variable that contained the name of the superglobal

  • 0

I was trying to pass a variable that contained the name of the superglobal array I wanted a function to process, but I couldn’t get it to work, it would just claim the variable in question didn’t exist and return null.

I’ve simplified my test case down to the following code:

function accessSession ($sessName)
{
    var_dump ($$sessName);
}

$sessName   = '_SERVER';

var_dump ($$sessName);

accessSession ($sessName);

The var_dump outside of the function returns the contents of $_SERVER, as expected. However, the var_dump in the function triggers the error mentioned above.

Adding global $_SERVER to the function didn’t make the error go away, but assigning $_SERVER to another variable and making that variable global did work (see below)

function accessSession ($sessName)
{
    global $test;
    var_dump ($$sessName);
}

$test       = $_SERVER;
$sessName   = 'test';

var_dump ($$sessName);

accessSession ($sessName);

Is this a PHP bug, or am I just doing something wrong?

  • 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-31T06:26:00+00:00Added an answer on May 31, 2026 at 6:26 am

    PHP: Variable variables – Manual

    Warning

    Please note that variable variables cannot be used with PHP’s Superglobal arrays within functions or class methods. The variable $this is also a special variable that cannot be referenced dynamically.


    Solutions

    function access_global_v1 ($var) {
      global    $$var;
      var_dump ($$var);
    }
    

    function access_global_v2 ($var) {
      var_dump ($GLOBALS[$var]);
    }
    

    $test = 123;
    
    access_global_v1 ('_SERVER');
    access_global_v2 ('test');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

basically I'm trying to pass a variable that is defined in a JSON array
I'm trying to pass a variable from a child form that contains a DataGridView,
I am trying to pass a variable PIN2 that I am retrieving like below
I am trying to pass a php variable into a java script window.location that
I am trying to pass one variable to a jQuery function inline (i.e.: using
i am trying to pass in a selector into the ID variable but this
I'm trying to pass a variable from my main swf to another one that's
I am trying to pass variable in the url but without success. In Drupal
I am trying to pass an integer array to a baseadapter, such that A.class
I'm trying to pass the arguments variable of a function as separate arguments to

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.