Last active
November 26, 2020 03:58
-
-
Save nissuk/954926 to your computer and use it in GitHub Desktop.
PHP: Noticeエラーを回避してarrayから値を取得するいくつかの例
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
error_reporting(E_ALL); | |
// 通常の取得 | |
// 配列のキーに値がない場合、Notice: Undefined index が発生します。 | |
$foo = $_GET['foo']; | |
$bar = $_GET['bar']; | |
$baz = $_GET['baz']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
error_reporting(E_ALL); | |
// A. エラー制御演算子を使ってNoticeエラーを回避します。 | |
// 利点: 簡単 | |
// 欠点: | |
// - デフォルト値を指定できない(NULL固定) | |
// - すべてのエラーを握りつぶしてしまう | |
$foo = @$_GET['foo']; | |
$bar = @$_GET['bar']; | |
$baz = @$_GET['baz']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
error_reporting(E_ALL); | |
// B. isset()と三項演算子を使用してNoticeエラーを回避します。 | |
// isset()は言語構造のため、isset()上ではNoticeエラーは出ません。 | |
// 利点: デフォルト値を指定できる | |
// 欠点: 冗長(下記の例だと$_GET[x]が2度出てくる) | |
$foo = isset($_GET['foo']) ? $_GET['foo'] : ''; | |
$bar = isset($_GET['bar']) ? $_GET['bar'] : ''; | |
$baz = isset($_GET['baz']) ? $_GET['baz'] : ''; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
error_reporting(E_ALL); | |
// C. B(isset()による取得)を簡略にする関数を定義、使用してNoticeエラーを回避します。 | |
// 利点: やや簡単。デフォルト値を指定できる | |
// 欠点: | |
// - シンタックスが変わる($_GET['foo'] 部分を ($_GET, 'foo') にする)のがやや嫌 | |
// - ユーザー関数を定義するため他の作業者とコンセンサスを取る必要がある | |
$foo = array_get($_GET, 'foo'); | |
$bar = array_get($_GET, 'bar'); | |
$baz = array_get($_GET, 'baz'); | |
function array_get($array, $key, $default = '') { | |
return isset($array[$key]) ? $array[$key] : $default; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
error_reporting(E_ALL); | |
// D. array_merge()を使用してデフォルト値を指定し、Noticeエラーを回避します。 | |
// 利点: 簡単。デフォルト値を指定できる | |
// 欠点: 配列に手を加えている | |
$_GET = array_merge(array( | |
'foo' => '', | |
'bar' => '', | |
'baz' => '', | |
), $_GET); | |
$foo = $_GET['foo']; | |
$bar = $_GET['bar']; | |
$baz = $_GET['baz']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
error_reporting(E_ALL); | |
// E. 配列演算子の結合(+)を使用してデフォルト値を指定し、Noticeエラーを回避します。 | |
// 利点: 一番簡単。デフォルト値を指定できる | |
// 欠点: PHPの配列の結合(+)の「元の配列に重複キーがあるものは上書きされない」 | |
// という事実が直感的でないため他の作業者に分かりづらい可能性がある / 配列に手を加えている | |
$_GET += array( | |
'foo' => '', | |
'bar' => '', | |
'baz' => '', | |
); | |
$foo = $_GET['foo']; | |
$bar = $_GET['bar']; | |
$baz = $_GET['baz']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
error_reporting(E_ALL); | |
// F. C.と別解の関数を定義してNoticeエラーを回避します。 | |
// これはutil.php( http://brandonwamboldt.github.io/utilphp/ )が採用していた方法で、 | |
// 引数をリファレンス渡し(&)として定義するとNoticeが出ないことを利用しています。 | |
// | |
// 利点: C.の関数よりもシンタックスが変わらない | |
// 欠点: | |
// - Noticeエラーが出ないのかぱっと見でわかりづらい | |
// - ユーザー関数を定義するため他の作業者とコンセンサスを取る必要がある(C.と同じ) | |
$foo = array_get($_GET['foo']); | |
$bar = array_get($_GET['bar']); | |
$baz = array_get($_GET['baz']); | |
function array_get(&$value, $default = '') { | |
return isset($value) ? $value : $default; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
error_reporting(E_ALL); | |
// 番外. PHP 5.3以上で使用可能な三項演算子(?:)※を使用する。 | |
// この方法ではNoticeエラーは回避できません。 | |
// ※ 実際には三項ではないですがPHPマニュアルにはTenary Operator(三項演算子)とあるため「三項演算子」としています。 | |
// ( http://php.net/manual/ja/language.operators.comparison.php#language.operators.comparison.ternary ) | |
// エルビス演算子とも呼ばれます。 | |
$foo = $_GET['foo'] ?: ''; | |
$bar = $_GET['bar'] ?: ''; | |
$baz = $_GET['baz'] ?: ''; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
error_reporting(E_ALL); | |
// 番外. PHP7以上で使用可能なnull合体演算子(??)を使用する。 | |
// PHP7以上が利用可能になればこれが一番よいと思います。 | |
// ( 参考: http://qiita.com/hnw/items/dff62cd02c780b613d03 ) | |
$foo = $_GET['foo'] ?? ''; | |
$bar = $_GET['bar'] ?? ''; | |
$baz = $_GET['baz'] ?? ''; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment