Among $_REQUEST, $_GET and $_POST which one is the fastest?

后端 未结 15 2441
小鲜肉
小鲜肉 2020-11-22 05:04

Which of these code will be faster?

$temp = $_REQUEST[\'s\'];

or

if (isset($_GET[\'s\'])) {
  $temp = $_GET[\'s\'];
}
else          


        
15条回答
  •  遥遥无期
    2020-11-22 05:29

    It's ugly and I wouldn't recommended it as a final solution when pushing code live, but while building rest functions, it's sometimes handy to have a 'catch-all' parameter grabber:

    public static function parseParams() {
        $params = array();
        switch($_SERVER['REQUEST_METHOD']) {
            case "PUT":
            case "DELETE":
                parse_str(file_get_contents('php://input'), $params);
                $GLOBALS["_{$_SERVER['REQUEST_METHOD']}"] = $params;
                break;
            case "GET":
                $params = $_GET;
                break;
            case "POST":
                $params = $_POST;
                break;
            default:
                $params = $_REQUEST;
                break;
        }
        return $params;
    }
    

    Someone creative could probably even add to it to handle command line parameters or whatever comes from your IDE. Once you decide what a given rest-function is doing, you can pick one appropriate for that given call to make sure you get what you need for the deploy version. This assumes 'REQUEST_METHOD' is set.

提交回复
热议问题