我在 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
需要一个字符串,并且该函数在传递数组时可能无法正常工作。它与安全无关。
关于php - 为什么要检查数组的值是否为数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12919009/