我收到这个错误:
“PHP Parse错误:语法错误,第66行/ var / www / vhosts / …中的意外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;
}
接着就,随即 :
>静态变量用常量初始化
>如果其值为常量值,则指定动态值.
相关文章
- laravel-5 - Laravel解析错误:语法错误,意外的T_CLASS,期待T_STRING或T_VARIABLE
- Haskell的新手,不明白我为什么会遇到无限类型的错误
- 数组 - 我收到错误“array.sh:3:array.sh:语法错误:”(“意外”
- javascript - 为什么我收到错误“错误:语法错误,无法识别的表达式:不支持的伪:选择”?
- 解析错误:语法错误,我的PHP代码中意外的文件结束
- 为什么我收到PHP致命错误:未捕获错误:找不到类'MyClass'?
- 为什么我收到这个MySQL错误 - '你的SQL语法有错误......'?
- php - Laravel 5:解析错误:语法错误,意外'?',期待变量(T_VARIABLE)