Php 为什么要检查数组的值是否为数组?
在《PHP解决方案》第二版一书中,我遇到了以下代码Php 为什么要检查数组的值是否为数组?,php,Php,在《PHP解决方案》第二版一书中,我遇到了以下代码 <?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 ar
<?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\u数组($value)$价值:修剪($value)代码>
如果$value是数组中保存的值(用户从表单输入的内容),为什么确定该值是否为数组至关重要?它是为了安全吗?“为什么确定该值是否为数组至关重要?”
因为trim
需要字符串,而传递数组时函数可能无法工作。它与安全性无关。让我直截了当地说:PHP警告:trim()希望参数1是字符串,数组在命令行代码的第1行给出,并且澄清一下,如果输入字段的名称类似于fieldName[]
,那么该值可以是数组-您通常会在多选输入中看到这一点,谢谢大家!这很有意义,特别是在处理非用户编写的多选表单选择时,trim函数是完全不必要的。@GP通过请求参数输入的所有内容都是用户输入,应该按此方式处理。