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

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

    I use this,

    $request = (count($_REQUEST) > 1)?$_REQUEST:$_GET;
    

    the statement validates if $_REQUEST has more than one parameter (the first parameter in $_REQUEST will be the request uri which can be used when needed, some PHP packages wont return $_GET so check if its more than 1 go for $_GET, By default, it will be $_POST.

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

    You are prematurely optimizing. Also, you should really put some thought into whether GET should be used for stuff you're POST-ing, for security reasons.

    0 讨论(0)
  • 2020-11-22 05:36
    if (isset($_GET['s'])) {
      $temp = $_GET['s'];
    }
    else {
      $temp = $_POST['s'];
    }
    

    Use that because it is safer and it won't make noticeable speed difference

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

    $_REQUEST, by default, contains the contents of $_GET, $_POST and $_COOKIE.

    But it's only a default, which depends on variables_order ; and not sure you want to work with cookies.

    If I had to choose, I would probably not use $_REQUEST, and I would choose $_GET or $_POST -- depending on what my application should do (i.e. one or the other, but not both) : generally speaking :

    • You should use $_GET when someone is requesting data from your application.
    • And you should use $_POST when someone is pushing (inserting or updating ; or deleting) data to your application.

    Either way, there will not be much of a difference about performances : the difference will be negligible, compared to what the rest of your script will do.

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

    I would use $_POST, and $_GET because differently from $_REQUEST their content is not influenced by variables_order.
    When to use $_POST and $_GET depends on what kind of operation is being executed. An operation that changes the data handled from the server should be done through a POST request, while the other operations should be done through a GET request. To make an example, an operation that deletes a user account should not be directly executed after the user click on a link, while viewing an image can be done through a link.

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

    I would use the second method as it is more explicit. Otherwise you don't know where the variables are coming from.

    Why do you need to check both GET and POST anyway? Surely using one or the other only makes more sense.

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