LoginSignup
7
1

More than 5 years have passed since last update.

unset()の前にisset()する必要はない

Last updated at Posted at 2018-08-03

未定義変数をいきなり使わないよう、isset()でチェックする心がけは良い習慣ですが
unset()する場合はisset()でチェックしなくてもUndefined VariableのNoticeにはなりません。

unset.php
<?php
error_reporting(E_ALL);
echo $foo;   //未定義の $fooを参照
unset($foo); //未定義の $fooをいきなりunset
echo 'Done.';

実行結果

$ php unset.php
PHP Notice:  Undefined variable: foo in /path/to/test.php on line 3  # echo はNoticeがでる
Done.            # unset は Noticeでない

おまけ:unset()は複数引数とれるので、まとめて処理できます。

<?php
unset($foo, $bar, $baz);
7
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
7
1