3

本 PHP Solutions, 2nd Edition で次のコードに出くわしました

<?php
foreach ($_POST as $key => $value) {
    // assign to temporary variable and strip whitespace if not an array
    $temp = is_array($value) ? $value : trim($value);
    // if empty and required, add to $missing array
    if (empty($temp) && in_array($key, $required)) {
        $missing[] = $key;
    } if (in_array($key, $expected)) {
        // otherwise, assign to a variable of the same name as $key
        ${$key} = $temp;
    }
}
?>

私の質問は、この行に関するものです:

$temp = is_array($value) ? $value : trim($value);

$value が配列に保持されている値 (ユーザーがフォームから入力したコンテンツ) である場合、値が配列であるかどうかを判断することが重要なのはなぜですか? セキュリティのためですか?

4

1 に答える 1

5

「値が配列かどうかを判断することが重要なのはなぜですか?」

trimは文字列を想定しており、配列を渡すと関数が機能しない可能性があるためです。セキュリティとは関係ありません。

于 2012-10-16T16:16:10.527 に答える