I'm working on a web library for personal use. I thought I'd be all clever and use variable variables to make my library support all request methods easily.
I had:
$request = '_' . $_SERVER['REQUEST_METHOD'];
$request = $$request;
But I get:
Undefined variable: _POST
Printed to my php error log.
I was just curious as to whether my idea is flawed conceptially, as well as why the logic fails to work when the following does:
$_a = 'b';
$b = '_a';
$c = $$b;
Edit:
The following does work:
$request = $_SERVER['REQUEST_METHOD'] === 'POST' ? $_POST : $_GET;
Duplicate of: Superglobals can't be accessed via variable variables in a function?
To fix I did:
$request = $GLOBALS['_' . $_SERVER['REQUEST_METHOD']];
You could also use my original code outside of a function or class.