我收到一个“语法错误,意外的 T_VARIABLE"错误.我不明白我做错了什么? [英] I'm getting a "syntax error, unexpected T_VARIABLE" error. I don't see what I'm doing wrong?

查看:24
本文介绍了我收到一个“语法错误,意外的 T_VARIABLE"错误.我不明白我做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误:PHP 解析错误:语法错误,第 66 行/var/www/vhosts/... 中出现意外的 T_VARIABLE"

这是我的代码:

function combine($charArr, $k) {$currentsize = sizeof($charArr);静态 $combs = array();静态 $originalsize = $currentsize;###### <-- 第 66 行 ######静态 $firstcall = true;如果 ($originalsize >= $k) {# 获取第一个组合$comb = '';if ($firstcall) {//如果这是第一次调用for ($i = $originalsize-$k; $i < $originalsize; $i++) {$comb .= $charArr[$i];}$combs[] = $comb;//将第一个组合附加到输出数组$firstcall = false;//我们只想在第一次迭代时这样做}........}

知道出了什么问题吗?

解决方案

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

<块引用>

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

你正在使用这个:

static $originalsize = $currentsize;

用表达式初始化——而不是常量.


这是手册部分关于静态变量的说法完全相同:

<块引用>

静态变量可以声明为在上面的例子中看到.尝试去为这些变量赋值是表达式的结果将导致解析错误.

为了以防万一,这里是关于表达式.>


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

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

这样:

  • 静态变量用常量初始化
  • 如果其值为常量,则分配动态值.

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?

解决方案

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

这篇关于我收到一个“语法错误,意外的 T_VARIABLE"错误.我不明白我做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆