Skip to content
Advertisement

PHP $_GET/$_POST via variable variables

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?

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement