我收到此错误:
“PHP 解析错误:语法错误,/var/www/vhosts/... 中第 66 行出现意外的 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
}
....
....
}
知道出了什么问题吗?
Quoting 手册 (该页面是关于静态属性的,但这同样适用于变量) :
与任何其他 PHP 静态变量一样,static特性可能只是
使用文字或初始化
持续的;表达式不是
允许。所以虽然你可以初始化
静态属性为整数或
数组(例如),你可能不会
将其初始化为另一个变量,以
函数返回值,或
目的。
你正在使用这个:
static $originalsize = $currentsize;
这是用表达式初始化的——而不是常量。
And here's [the manual's section][2] that says quite the same about static variables :
静态变量可以声明为
在上面的例子中看到。尝试去
为这些变量赋值
是表达式 will 的结果
导致解析错误。
而且,为了以防万一,这里是关于表达式.
In your case, to avoid that problem, I suppose you could modify your code, so it looks like this :
$currentsize = sizeof($charArr);
static $originalsize = null;
if ($originalsize === null) {
$originalsize = $currentsize;
}
接着就,随即 :
- 静态变量用常量初始化
- 如果其值为常量,则分配动态值。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)