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

后端 未结 15 2243
小鲜肉
小鲜肉 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:28

    $_GET retrieves variables from the querystring, or your URL.>

    $_POST retrieves variables from a POST method, such as (generally) forms.

    $_REQUEST is a merging of $_GET and $_POST where $_POST overrides $_GET. Good to use $_REQUEST on self refrential forms for validations.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 05:30

    Don't worry. But you should still use the second solution (plus an extra check for none of those variables existing), because there are security issues with $_REQUEST (since $_GET and $_POST aren't the only sources for that array).

    There was a post about the problems with $_REQUEST yesterday, I believe. Let me go find it.

    EDIT: Oh well, not directly a post, but here it is anyway: http://kuza55.blogspot.com/2006/03/request-variable-fixation.html

    0 讨论(0)
  • 2020-11-22 05:31

    GET vs. POST

    1) Both GET and POST create an array (e.g. array( key => value, key2 => value2, key3 => value3, ...)). This array holds key/value pairs, where keys are the names of the form controls and values are the input data from the user.

    2) Both GET and POST are treated as $_GET and $_POST. These are superglobals, which means that they are always accessible, regardless of scope - and you can access them from any function, class or file without having to do anything special.

    3) $_GET is an array of variables passed to the current script via the URL parameters.

    4) $_POST is an array of variables passed to the current script via the HTTP POST method.

    When to use GET?

    Information sent from a form with the GET method is visible to everyone (all variable names and values are displayed in the URL). GET also has limits on the amount of information to send. The limitation is about 2000 characters. However, because the variables are displayed in the URL, it is possible to bookmark the page. This can be useful in some cases.

    GET may be used for sending non-sensitive data.

    Note: GET should NEVER be used for sending passwords or other sensitive information!

    When to use POST?

    Information sent from a form with the POST method is invisible to others (all names/values are embedded within the body of the HTTP request) and has no limits on the amount of information to send.

    Moreover POST supports advanced functionality such as support for multi-part binary input while uploading files to server.

    However, because the variables are not displayed in the URL, it is not possible to bookmark the page.

    0 讨论(0)
  • 2020-11-22 05:31

    Use REQUEST. Nobody cares about the speed of such a simple operation, and it's much cleaner code.

    0 讨论(0)
  • 2020-11-22 05:34

    $_GET retrieves variables from the querystring, or your URL.>

    $_POST retrieves variables from a POST method, such as (generally) forms.

    $_REQUEST is a merging of $_GET and $_POST where $_POST overrides $_GET. Good to use $_REQUEST on self refrential forms for validations.

    0 讨论(0)
提交回复
热议问题