6

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

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

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

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

?>
8
  • 4
    And which is line xx ? Commented Jul 30, 2013 at 0:46
  • 2
    Usually errors like this are due to unbalanced quotes, or a missing ; somewhere. Commented Jul 30, 2013 at 0:47
  • look at the line preceeding xx in /path/to/file.php for something missing Commented Jul 30, 2013 at 0:47
  • 2
    Or it's due to using var $x=1; outside of a class declaration. Commented Jul 30, 2013 at 0:47
  • 1
    possible duplicate of T_VAR Error with PHP Commented Jul 30, 2013 at 0:50

4 Answers 4

15

Keyword var is 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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yep, that was my problem. I didn't realise var was for classes only (this wasn't inside a class). Derp.
7

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

Comments

2

Without seeing all the sources involved, I'm guessing you're combining JavaScript and 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'.

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

echo "var myJavasScriptVar = '$phpVar'";

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

Hope this helps!

Comments

0

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

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

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

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.