dongqiaozhe5070 2018-06-29 22:16
浏览 143
已采纳

由于引用变量传递,为foreach提供了无效参数

I am upgrading a codebase that makes use of pass by reference Main function

    function splitSqlFile(&$ret, $sql)
    {
       $sql               = trim($sql);
       $sql_len           = strlen($sql);
       $char              = '';
       $string_start      = '';
       $in_string         = false;

      for ($i = 0; $i < $sql_len; ++$i) {
           $char = $sql[$i];
        if ($in_string) {
            for (;;) {
                $i = strpos($sql, $string_start, $i);
                if (!$i) {
                    $ret[] = $sql;
                    return true;
                }else if ($string_start == '`' || $sql[$i-1] != '\\'){
                    ......
                }else {
                    ......
                } // end if...elseif...else
            } // end for
        }
        else if ($char == ';') {
            $ret[]    = substr($sql, 0, $i);
            $sql      = ltrim(substr($sql, min($i + 1, $sql_len)));
            $sql_len  = strlen($sql);
            if ($sql_len) {
                $i = -1;
            } else {
                // The submited statement(s) end(s) here
                return true;
            }
        }else if (($char == '"') || ($char == '\'') || ($char == '`')) {
            $in_string    = true;
            $string_start = $char;
        } // end else if (is start of string)

        // for start of a comment (and remove this comment if found)...
        else if ($char == '#' || ($char == ' ' && $i > 1 && $sql[$i-2] . $sql[$i-1] == '--')) {
            ......
            if (!$end_of_comment) {
            // no eol found after '#', add the parsed part to the returned
            // array and exit
                $ret[]   = trim(substr($sql, 0, $i-1));
                return true;
            } else {
                .....
            } // end if...else
        } // end else if (is comment)
    } // end for

    // add any rest to the returned array
    if (!empty($sql) && trim($sql) != '') {
        $ret[] = $sql;
    }
    return true;
}

Calling the function

    $sqlUtility->splitSqlFile($pieces, $sql_query);
    foreach ($pieces as $piece) 
    {
      .......
    }

If the above variable splitSqlFile(&$ret, $sql) have the "&" before it, the program does run successfully, but if it is removed, now splitSqlFile($ret, $sql), It will start returning the 'invalid argument supplied for foreach' error.and when I try using the "is_array" function to check if it is an array, the result is always "NULL".

  • 写回答

1条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 数据库原理怎么从文字描述中的句子推出(分析)函数依赖?
      • ¥15 yoj获取密码 队列 解密 C语言
      • ¥15 想要使用有序logistic模型,因变量收集结果为以下,可以继续分析吗?
      • ¥15 C语言的问题 求尽快解答一下
      • ¥15 sql相关子查询的一道选择题
      • ¥15 关于图像检测的问题#
      • ¥15 用cemu8086编写代码
      • ¥15 mcisendcommand播放音乐堆栈调用异常
      • ¥100 R语言的数据分析及可视化
      • ¥200 opc-62541库的密码登录和加密连接问题