Reputation: 178
I want to change the request method according to variable, like in the example:
$input = "GET me out of here!";
$method = strtoupper($input != "It should be POST" ? "get" : "post");
$exists = isset($_${$method}["some-variable"]) && $_${$method}["some-variable"] == "I can be both get and post!";
Is it possible / valid?
Upvotes: 0
Views: 36
Reputation: 64657
You can do $method = '_POST';
and then do $$method
to access $_POST
:
http://sandbox.onlinephpfunctions.com/code/45381e507fe957cabb751e4bd62a71418a7bb45f
$input = "GET me out of here!";
$_GET = ["some-variable" => "I can be both get and post!"];
$method = strtoupper($input != "It should be POST" ? "_GET" : "_POST");
$exists = isset($$method["some-variable"]) && $$method["some-variable"] == "I can be both get and post!";
echo $exists ? "exists" : "nope"; // exists
That being said, you could also just use $_REQUEST
with the caveat that it also includes $_COOKIE
Upvotes: 1