LoginSignup
2
1

More than 1 year has passed since last update.

空文字をexplode()で分割した時の返り値

Last updated at Posted at 2018-04-25

PHPで explode() 使う時に第2引数に空文字入ってるとどうなるんだ?と思い調べてみましたが、
マニュアルの該当箇所分かりづらかったので、残しておきます。結構初心者向けです。

#サンプルコード
下記コードで返り値を確かめます。

$str = '';
$res = explode(',', $str);
print_r($res);

( PHPマニュアル より引用 )

#返り値
結果は以下の通りです。

array(1) {
    [0] => 
    string(0) ""
}

keyとして0を、valueとして空の文字列を持つ1要素の配列が返されます。

え、なんで1要素いれた!? 空配列でいいじゃん!!

ちなみに私は、この後 $resempty() で判定しようとして失敗しましたw

#参考
PHPマニュアル:http://php.net/manual/ja/function.explode.php#99830

2
1
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
2
1