4

I'm attempting to dynamically access both the $_GET and $_POST arrays, among others, using variable variables. The reason I'm trying to do this is so that I can perform similar actions on multiple arrays without needing to declare specific iterations for each. I'm reasonably sure this is possible, as PHP's documentation says it is able to use variable variables to dynamically access an array, however I'm unable to get it to work. A simple demonstration is when I'm attempting to verify that a certain property has been set.

if(isset(${$this->_array}[$property])) { return ${$this->_array}[$property]; }
else { return null; }

When I run the above script I always get null, however when I statically seek the desired property, manually using $_GET or $_POST, I get the desired outcome. I have triple checked $this->_array and $property and they are returning the correct string values. Are these arrays unavailable for such access, or am I doing something wrong?

4
  • 3
    en.wikipedia.org/wiki/Code_smell Commented Mar 13, 2012 at 20:35
  • @ceejayoz: OK? Heard of it before, familiarized myself with the concept. What are you trying to say? That my code is wrong somewhere else? I've already determined that the variables being passed to the above statement are correct. Not sure what else it could be. Commented Mar 13, 2012 at 20:41
  • I'm trying to say that using variable variables is usually a sign that you're doing something wrong. There's almost always a better way that won't make subsequent developers want to kill you. Commented Mar 13, 2012 at 20:46
  • Alright, I didn't realize variable variables were such taboo, this was my first experience with them. I'll be sure to keep them out of future code :) Commented Mar 13, 2012 at 20:55

7 Answers 7

7

Superglobals (such as $_POST) can not be used as variable variables within functions.

You could say something like $post = $_POST; and then use 'post' and it'd work, but directly using '_POST' won't.

Sign up to request clarification or add additional context in comments.

Comments

4

Superglobals cannot be referenced as variable variables inside of classes or methods, so this will work:

<?php
$var = "_GET";
print_r(${$var});

But this will not:

<?php
test();
function test() {
  $var = "_GET";
  print_r(${$var});
}

I suspect that there is a better way to do what you are trying to accomplish.

http://php.net/manual/en/language.variables.superglobals.php#refsect1-language.variables.superglobals-notes

2 Comments

Not when not even this worked. I did try just printing the whole array, forgot to mention that. Thanks though
My code did work. I updated my answer to explain why it didn't work for you.
3

Whatever you're doing wrong, using variable variables is probably making it worse. For your own sanity, please stop. They should never be deployed in production code under any circumstances. They're impossible to debug, and using them in your code is like trying to read something that someone else wrote with their feet. If they have particularly dexterous feet, then perhaps you can understand what they're doing. But 99.9999% of the time, it is better to just use normal arrays.

That being said, try $_REQUEST instead.

Comments

2

There's already an array that contains both $_GET and $_POST. It's named $_REQUEST. Having said that, it can also contain the contents of $_COOKIE depending on the request_order setting, but the default is just $_GET and $_POST.

Comments

0

You say you want to access both the $_GET and $_POST arrays, among others -- what are these 'others'? You can use $_REQUEST to check the contents of $_GET, $_POST, and $_COOKIE all at once.

Comments

0

you can do this but dont know if it is a good coding practice

if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
     $method = '_POST';
 }
 else {
    $method = '_GET';
 }
 $data = $$method;

Comments

0

You can create an associative array that references both arrays, and use that.

$params = [
    '_GET' => $_GET,
    '_POST' => $_POST
];

Then you can use

return $params[$this->_array][$property] ?? null;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.