【问题标题】:Warning: strlen() expects parameter 1 to be string, array given in /includes/functions/general.php on line 159警告:strlen() 期望参数 1 是字符串,数组在第 159 行的 /includes/functions/general.php 中给出
【发布时间】:2018-04-27 23:01:53
【问题描述】:

我有这个错误:

警告:strlen() 期望参数 1 为字符串,数组在 /includes/functions/general.php 第 159 行给出

在文件中:

function tep_get_all_get_params($exclude_array = '') {
    global $HTTP_GET_VARS;

    if (!is_array($exclude_array)) $exclude_array = array();

    $get_url = '';
    if (is_array($HTTP_GET_VARS) && (sizeof($HTTP_GET_VARS) > 0)) {
      reset($HTTP_GET_VARS);
      while (list($key, $value) = each($HTTP_GET_VARS)) {
        if ( (strlen($value) > 0) && ($key != tep_session_name()) && ($key != 'error') && (!in_array($key, $exclude_array)) && ($key != 'x') && ($key != 'y') ) { // THIS IS 159 LINE
          $get_url .= $key . '=' . rawurlencode(stripslashes($value)) . '&';
        }
      }
    }

    return $get_url;
  }

有人可以帮我解决这个问题吗?

【问题讨论】:

  • 错误提示 $value 是一个数组(“给定数组”),而它应该是一个字符串(“期望参数 1 是字符串”)。为什么会这样?
  • 为防止混淆,请考虑使用count 而不是sizeof, it other programming languages sizeof` 意味着完全不同的东西。
  • 对于这样的代码,设置和具有调试功能的 IDE 通常是有利的,然后通过任何调用和迭代来了解正在发生的事情。 -- 看起来它也是为古老的 PHP4 版本编写的,可能需要彻底检修。
  • 那么你能写信给我吗,我应该在代码中更改什么以避免错误?我是可怜的php..
  • 停止使用$HTTP_GET_VARS 它已被弃用。 php.net/manual/en/reserved.variables.get.php

标签: php string parameters warnings strlen


【解决方案1】:

问题在于您致电each。它返回一个包含 4 个元素的数组:

元素 0 和 key 包含数组元素的键名,1 和 value 包含数据。

这意味着 $key 被分配了键名,正如您所期望的那样,但 $value 被分配了一个包含 3 个元素和索引为 'key', 1, and 'value' 的数组。

解决此问题的最简单方法是更改​​此行:

while (list($key, $value) = each($HTTP_GET_VARS)) {

foreach ($HTTP_GET_VARS as $key => $value) {

请注意,您可以删除该行:

reset($HTTP_GET_VARS);

因为在使用 foreach 时不需要它。另请注意,$HTTP_GET_VARS 自 PHP4.1.0 起已弃用,您应将其替换为 $_GET

【讨论】:

  • 听起来你的 GET 变量之一也必须是一个数组?你能做一个var_dump($_GET) 并发布吗?另外,如果您可以使用当前代码编辑您的帖子,那将很有帮助。
猜你喜欢
  • 2015-04-29
  • 1970-01-01
  • 1970-01-01
  • 2013-12-11
  • 2019-06-21
  • 2016-10-10
  • 1970-01-01
  • 2013-11-24
  • 1970-01-01
相关资源
最近更新 更多