35
32

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.

「NULLは配列」「なにをいってるんだお前は」

Posted at

以下みたいなコードを書いていて

$values = [
'a'=>null,
'b'=>'abc',
];
foreach($values as $key => &$value)
{   // 単純にコードがおかしいのだが、二週目にエラーなので気付かず
    $value[$key] = strtoupper($value);
    // ホントはこう書きたかったのだ
    // $value = strtoupper($value);
}

以下のような地雷を踏んで久々に笑顔になりました。


$ng = 1;
$ng['abc'] = 2; // PHP Warning:  Cannot use a scalar value as an array 

$ok = null;
$ok['abc'] = 2; // => ['abc'=>'2']

「NULLは配列!」「なにいってるんだお前は」

なるほど〜😀

ところで最近は以下みたいな記述ができるようになったのですが、これもいつかは踏みそうだなとおもっています。

echo "abc"[2];
// => c

こちらからは以上です。

35
32
3

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
35
32

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?