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

发布于 2024-10-19 06:56:44 字数 791 浏览 3 评论 0原文

我收到此错误: “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
        }
    ....
    ....
}

知道出了什么问题吗?

I'm getting this error:
"PHP Parse error: syntax error, unexpected T_VARIABLE in /var/www/vhosts/... on line 66"

Here's my code:

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

Any idea what's wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

疧_╮線 2024-10-26 06:56:44

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

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


您正在使用这个:

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

这样:

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

Quoting the manual (that page is about static properties, but the same applies for variables) :

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.

You are using this :

static $originalsize = $currentsize;

Which is initializing with an expression -- and not a constant.

And here's [the manual's section][2] that says quite the same about static variables :

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.

And, just in case, here's about expressions.

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

With that :

  • The static variable is initialized with a constant
  • If its value is the constant one, assign the dynamic value.
世界等同你 2024-10-26 06:56:44
static $originalsize = $currentsize; ###### <-- LINE 66 ######

您不能将变量作为静态变量的默认值传递。相反,请执行以下操作:

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

You can't pass a variable as the default value of a static variable. Instead, do the following:

static $originalsize;
$originalsize = $currentsize;
莫多说 2024-10-26 06:56:44

引用 php 手册

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

To quote the php 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.

耳根太软 2024-10-26 06:56:44

来自 php 手册

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

From php 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文