我收到一个“语法错误,意外的 T

编程入门 行业动态 更新时间:2024-10-15 12:30:45
本文介绍了我收到一个“语法错误,意外的 T_VARIABLE"错误.我不明白我做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我收到此错误: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屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 13:08:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1410458.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:意外   语法错误

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!