PHP 意外的“var”(T_VARIABLE)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17936577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:41:18  来源:igfitidea点击:

PHP Unexpected 'var' (T_VARIABLE)

php

提问by ZephireNZ

For whatever reason, I have a small bit of PHP code that no matter where I put var $blah;, it always gives this error in the logs: PHP?Parse?error:??syntax?error,?unexpected?'var'?(T_VAR)?in?/path/to/file.php on line xx

无论出于何种原因,我都有一小段 PHP 代码,无论我把var $blah;它放在哪里,它总是在日志中给出这个错误:PHP?Parse?error:??syntax?error,?unexpected?'var'?(T_VAR)?in?/path/to/file.php on line xx

I have no idea why it wouldn't accept this. A class that is included (which creates the $proverbSitevariable in another php section) uses plenty of 'var $blah', with no problems. I also realise this is probably just an embarassingly simple mistake.

我不知道为什么它不接受这一点。包含的类($proverbSite在另一个 php 部分创建变量)使用了大量的“var $blah”,没有问题。我也意识到这可能只是一个非常简单的错误。

<?php
        $proverbSite->dbConnect();
        $result = $proverbSite->dbQuery("randProverb");

        if($result != null) {
            $row = $result->fetch_assoc();
            echo $row['proverb'];
            echo "<br>";
        }

?>

回答by Lex

Keyword varis only used in classes (in PHP). In the plain scope variables are automatically declared as you mention them. Just erase it, and it should work.

关键字var仅在类中使用(在 PHP 中)。在普通范围中,变量会在您提到它们时自动声明。只需擦除它,它应该可以工作。

回答by PHPman

Check the line before xxbecause you may have forgotten a ;and this may cause PHP to interpret it incorrectly

检查之前的行,xx因为您可能忘记了 a;这可能会导致 PHP 错误地解释它

回答by stephenspann

Without seeing all the sources involved, I'm guessing you're combining JavaScript and PHP.

没有看到所有涉及的来源,我猜你正在结合 JavaScript 和 PHP。

JavaScript variable declarations begin with 'var' and PHP does not... If PHP were to encounter the code 'var' before a variable, it would give the error message you listed. T_VAR usually indicates PHP is trying to interpret a constant, which would be the case with a JavaScript 'var'.

JavaScript 变量声明以“var”开头,而 PHP 不...如果 PHP 在变量之前遇到代码“var”,它会给出您列出的错误消息。T_VAR 通常表示 PHP 正在尝试解释一个常量,JavaScript 'var' 就是这种情况。

Now, as for the plugin/library your are using, it may be echoing javascript, but including a PHP variable, ex:

现在,对于您正在使用的插件/库,它可能会回显 javascript,但包括一个 PHP 变量,例如:

echo "var myJavasScriptVar = '$phpVar'";

In PHP, '$' in a string means that it will be replaced by a variable.

在 PHP 中,字符串中的 '$' 表示它将被变量替换。

Hope this helps!

希望这可以帮助!

回答by Mirza Sisic

I know that in newer versions of Laravel it works if you leave out the varkeyword, for example:

我知道在较新版本的 Laravel 中,如果您省略var关键字,它会起作用,例如:

Route::get('/read', function(){

   $results = DB::select('select * from posts where id = ?', [1]);
      return  dump($results);

});