You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP报错求助:strlen()期望参数为字符串却传入数组的问题

Fixing the "strlen() expects parameter 1 to be string, array given" Warning in PHP

Hey there, let's break down this warning you're seeing and fix it up. The error points straight to line 159 in your general.php file, where the strlen() function is being passed an array instead of a string. Here's what's going on and how to fix it:

What's Causing the Issue?

Your tep_get_all_get_params function loops through $HTTP_GET_VARS (which stores all URL query parameters) and checks if each value has a length greater than 0 with strlen($value). The problem arises when one of your GET parameters is an array—like if a URL looks something like ?category[]=electronics&category[]=clothing. When the loop hits that array value, strlen() throws a warning because it can only calculate the length of strings, not arrays.

Here's the problematic snippet from your code:

while (list($key, $value) = each($HTTP_GET_VARS)) {
    if ( (strlen($value) > 0) && ($key != tep_session_...

Solutions to Fix the Warning

You have a couple of options depending on whether you need to handle array parameters or not.

Option 1: Skip Array Values (Simplest Fix)

If your application doesn't need to pass array parameters through this function, just add a check to skip any values that are arrays before running strlen():

while (list($key, $value) = each($HTTP_GET_VARS)) {
    // Skip array values and empty strings
    if ( !is_array($value) && (strlen($value) > 0) && ($key != tep_session_name()) ) {
        // Continue with your existing logic here
    }
}

This will ignore any array parameters entirely, which is safe for most use cases where you're just passing single-value query params.

Option 2: Handle Array Values (If You Need Them)

If your app does use array GET parameters and you want to include them in the generated URL, you'll need to convert the array to a valid string format. Use http_build_query() to turn the array into a proper query string segment:

while (list($key, $value) = each($HTTP_GET_VARS)) {
    if ($key != tep_session_name()) {
        if (is_array($value)) {
            // Convert array to a valid query string part
            $array_query = http_build_query(array($key => $value));
            $get_url .= '&' . $array_query;
        } elseif (strlen($value) > 0) {
            // Handle regular string values as before
            $get_url .= '&' . tep_output_string($key) . '=' . tep_output_string($value);
        }
    }
}

Just make sure you're using tep_output_string (or your app's equivalent sanitization function) to prevent XSS vulnerabilities, same as you do with string values.

Bonus: Modernize Your Code

While you're at it, let's fix some deprecated stuff to avoid future issues:

  • Replace $HTTP_GET_VARS with $_GET—the former is deprecated in newer PHP versions.
  • Swap out the each() function (also deprecated) for a foreach loop, which is cleaner and more modern:
    foreach ($_GET as $key => $value) {
        // Your updated condition and logic here
    }
    

内容的提问来源于stack exchange,提问作者gold1s

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:14:36