1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PHP で GET, POST のどちらからでもパラメーターを受け取る

Last updated at Posted at 2019-06-07

GET, POST のどちらでパラメーター渡されても楽に受け取りたい

方法

function fetch_param($param)
{
    return $_SERVER['REQUEST_METHOD'] === 'GET'
        ? filter_input(INPUT_GET, $param)
        : filter_input(INPUT_POST, $param);
}

やってること

PHP のスーパーグローバル変数である $_REQUEST['REQUEST_METHOD'] を用いると、いまアクセスされている httpメソッドがGET なのか POST なのか確認できるので、それに応じて filter_input() の第一引数を変更する。

もうちょっとスマートな方法がありそうな気はしますね。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?