gpt4 book ai didi

php - 我收到 "syntax error, unexpected T_VARIABLE"错误。我不明白我做错了什么?

转载 作者:可可西里 更新时间:2023-11-01 00:07:06 24 4
gpt4 key购买 nike

我收到这个错误:“PHP 解析错误:语法错误,第 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 - 我收到 "syntax error, unexpected T_VARIABLE"错误。我不明白我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5122729/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com