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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:35:36+00:00 2026-05-16T11:35:36+00:00

As a web developer, I’m always using this approach to something like a login

  • 0

As a web developer, I’m always using this approach to something like a login form or other “save” operation (ignoring the dangers of directly accessing input variables):

if (isset($_POST['action']) && $_POST['action'] == 'login')
{
    // we're probably logging in, so let's process that here
}

To make this less tedious and keeping in line with DRY principles (sort of), I cooked this up:

function isset_and_is ($superglobal, $key, $value)
{
    $ref = '_' . strtoupper($superglobal);

    return isset($$ref[$key]) && $$ref[$key] == $value;
}

if (isset_and_is('post', 'action', 'login'))
{
    // we're probably logging in, so let's process that here
}

This fails miserably, despite my oh-so-clever use of dynamic variable names to access the superglobal.

So, I’m stuck using this ugly:

function isset_and_is ($superglobal, $key, $value)
{
    switch (strtoupper($superglobal))
    {
        case 'GET':     $ref =& $_GET;     break;
        case 'POST':    $ref =& $_POST;    break;
        case 'REQUEST': $ref =& $_REQUEST; break;
        default:        die('megafail');   return;
    }

    return isset($ref[$key]) && $ref[$key] == $value;
}

if (isset_and_is('post', 'action', 'login'))
{
    // we're probably logging in, so let's process that here
}

My question: Is there a way to dynamically access the superglobal variables like I’m attempting to do in my second code sample? If no, is there a better/more efficient way to accomplish what I’m doing in the third code sample?


My solution: Thanks to Tom Haigh’s answer, here’s the final code I’ll be going with:

function isset_and_is ($superglobal, $key, $value)
{
    $ref =& $GLOBALS['_' . strtoupper($superglobal)];

    return isset($ref[$key]) && $ref[$key] == $value;
}
  • 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-16T11:35:37+00:00Added an answer on May 16, 2026 at 11:35 am

    You can do it like this:

    function test($var) {
        //this 
        var_dump( $GLOBALS[$var] );
    
        //or this
        global $$var; //this is needed even for superglobals
        var_dump($$var);
    }
    
    test('_GET');
    

    so you could use something like this in your case

    function isset_and_is ($superglobal, $key, $value) {
        $var = '_' . $superglobal;
        return isset($GLOBALS[$var]) && ($GLOBALS[$var][$key] == $value);
    }
    
    $is_login = isset_and_is('GET', 'action', 'login');
    

    Or alternatively you can take the variable by reference and use isset(), e.g.

    function get_var(& $var) {
        if (isset($var)) {
            return $var;
        }
        return null;
    }
    
    //will not give you a notice if not set
    $post_var = get_var($_POST['var']);
    
    if (get_var($_GET['action']) == 'login') {
        //stuff
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Like every other web developer on the planet, I have an issue with users
I'm a web developer and now developing a swing application. I'm using Netbeans. Currently
I'm an amateur web developer and I would like to know how to create
As a web developer I am using PHP and I know that I have
[ASP .Net - Microsoft Visual Web Developer 2010] Hi all, I've problem with this
I'm using Web Developer 2010. I just created a resource file lang.resx with different
Usually I am a web developer so this is probably a very novice question.
I'm using Visual Web Developer 2010. I created a sql server connection to a
using Visual.Web.Developer.2010.Express; using SQL.Server.Management.Studios.R2.Express; What I'm trying to do, is get a button to
I am using Visual Web Developer 2008.I have debug my javascript at localhost by

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.