1
0

More than 3 years have passed since last update.

http_build_queryの落とし穴 備忘録

Posted at

NULLを送りたい時の落とし穴の内容

http_build_queryの仕様ではあるが、渡したパラメーター配列の値やキーによって、若干想定(希望w)と異なる結果を返してしまうことがある。

問題点は2つ:

「キーのみあって値がNULL」の場合、http_build_queryの返却値にパラメーターのキーが入らず、パラメーターとして欠けてしまう。

例:

$params = array('key1' => null, 'key2' => 'value2');
var_dump(http_build_query($params));

の場合の結果は

string 'key2=value2' (length=11)

になります。

野望的には

string 'key1=&key2=value2' (length=11)

を期待していても無駄です。

対策は、あとで追記しますが、まずは備忘録的にアップしました。

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