php – 我收到“语法错误,意外的T_VARIABLE”错误.我不明白我做错了什么?

我收到这个错误:
PHP Parse错误:语法错误,第66行/ var / www / vhosts / …中的意外T_VARIABLE”

这是我的代码:

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
        }
    ....
    ....
}

知道什么是错的吗?

最佳答案
引用the manual(该页面是关于静态属性,但同样适用于变量):

Like any other PHP static variable, static properties may only be
initialized using a literal or
constant; expressions are not
allowed
. So while you may initialize
a static property to an integer or
array (for instance), you may not
initialize it to another variable, to
a function return value, or to an
object.

你正在使用这个:

static $originalsize = $currentsize;

哪个用表达式初始化 – 而不是常量.

这里的the manual’s section对静态变量的说法完全相同:

Static variables may be declared as
seen in the examples above. Trying to
assign values to these variables which
are the result of expressions will
cause a parse error.

而且,以防万一,这里是about expressions.

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

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

接着就,随即 :

>静态变量用常量初始化
>如果其值为常量值,则指定动态值.

转载注明原文:php – 我收到“语法错误,意外的T_VARIABLE”错误.我不明白我做错了什么? - 代码日志