VARIABLE“错误,我不明白我做错了什么?

我得到这个错误:“PHP解析错误:语法错误,意外的T_VARIABLE在/ var / www / vhosts / ...在线66”

这是我的代码:

function combine($charArr, $k) {

    $currentsize = sizeof($charArr);
    static $combs = array();
    static $originalsize = $currentsize; ###### <-- LINE 66 ######
    static $firstcall = true;

    if ($originalsize >= $k) {

        # Get the First Combination 
        $comb = '';
        if ($firstcall) { //if this is first call
            for ($i = $originalsize-$k; $i < $originalsize; $i++) {
                $comb .= $charArr[$i];
            }
            $combs[] = $comb; //append the first combo to the output array
            $firstcall = false; //we only want to do this during the first iteration
        }
    ....
    ....
}

任何想法有什么不对?


引用手册(该页面是关于静态属性的,但同样适用于变量):

像任何其他PHP静态变量一样,静态属性只能使用文字或常量初始化; 表达式是不允许的 。 因此,虽然可以将静态属性初始化为整数或数组(例如),但不能将其初始化为其他变量,函数返回值或对象。

你正在使用这个:

static $originalsize = $currentsize;

用表达式初始化 - 而不是常量。


以下是关于静态变量的手册部分:

静态变量可以在上面的例子中声明。 试图为这些表达式结果的变量赋值会导致解析错误。

而且,为了以防万一,这里是关于表达式的。


在你的情况下,为了避免这个问题,我想你可以修改你的代码,所以它看起来像这样:

$currentsize = sizeof($charArr);
static $originalsize = null;
if ($originalsize === null) {
    $originalsize = $currentsize;
}

接着就,随即 :

  • 静态变量用一个常量初始化
  • 如果它的值是常量,则分配动态值。

  • static $originalsize = $currentsize; ###### <-- LINE 66 ######
    

    您无法传递一个变量作为静态变量的默认值。 相反,请执行以下操作:

    static $originalsize;
    $originalsize = $currentsize;
    

    引用php手册:

    像任何其他PHP静态变量一样,静态属性只能使用文字或常量初始化; 表达式是不允许的。 因此,虽然可以将静态属性初始化为整数或数组(例如), 但不能将其初始化为其他变量 ,函数返回值或对象。

    链接地址: http://www.djcxy.com/p/11963.html

    上一篇: VARIABLE" error. I don't see what I'm doing wrong?

    下一篇: VARIABLE, expecting ',' or ';' on line 29