php - 为什么要检查数组的值是否为数组?

标签 php

我在 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/

相关文章:

php - 查询在 sql 中有效,在 php 中无效

php - PHP 中的 "exec(' sh foo.sh ')"不工作

PHP fatal error : Class 'Facebook\\Entities\\AccessToken

php - 什么是 php-fpm 警告 "failed to acquire scoreboard"?

php - mysql数据库性能问题

php - mysql查询中的二维数组

php - php和mysql按年排列月份

java - 在 Tomcat/JSP 托管上编译 Java

php - 尝试转义 PHP MySQL 查询的单引号和双引号

php - 对两个表的 SQL 结果进行排序