Parse error problem: syntax error, Unexpected T_VARIABLE

Asked

Viewed 21,225 times

3

I’m having trouble inserting a variable into an array... I did some research here and I couldn’t find any similar question that could help me solve this, so I’m creating this.

The mistake: Parse error: syntax error, Unexpected T_VARIABLE points to line 26

On line 26 I have this:

var $users = array('user1' => $senha);

The variable $password that is causing the error...if I put any string works without error.

  • 2

    Put the rest of the code, you can’t help with just one line. The error depends on what you’re using.

  • this variable $senha was set before, certain?

  • And if you leave only $users = array('user1' => $password); without var?

  • 2

    You’re in a class?

  • 2

    Must be missing one ; at the end of the previous line

  • 2

    As already said checks if one is missing; at the end of the previous line and removes the word var

  • What version of php is using?

  • In addition to removing this "var", you can cast to convert the password type: $users = array('user1' => (string) $password);

Show 3 more comments

1 answer

10


The mistake Parse error: syntax error, Unexpected T_VARIABLE means that PHP was not expecting the definition of a variable at any given time.

This error may occur in some situations, but your question includes some special cases.


The most common problem is the absence of a semicolon in the previous line. The interpreter in this case informed the error on the next line that occurs, and not on the line where the semicolon is missing:

<?php

$var = 1
$var2 = 3;

Returns the error:

Parse error: syntax error, Unexpected '$var2' (T_VARIABLE) in path/file on line 4


Another problem is the definition of the word var before the variable in a context outside a class.

This keyword was used in PHP 4 to define class properties, and currently only exists to maintain backward compatibility with older codes.

In recent versions, he is a synonym for public.

If you are setting a variable simply remove the var.

$users = array('user1' => $senha);

Now, if you’re defining $user as a class property, the problem is that it is not possible to define dynamic values directly in the class definition (variables or functions basically), as can be observed in the documentation.

<?php
class SimpleClass
{
   // declaração de propriedade invalida:
   // ...
   public $var5 = $myVar;

}

That explains the fact that var $users = array('user1' => 'Blabla'); work.

In short

  1. See if the line above the one reported in the error ends with ;

  2. Do not use var in PHP 5 onwards, prefer private, protected or public.

  3. If you need to define dynamic values in the class property, you can do this by the constructor method:

    <?php
    class SimpleClass
    {
    
       public $users = array();
    
       public function __construct($senha){
           $this->users = array('user1' => $senha);
    
           // ...
       }
    
    }
    
  • The use of var was for php 4, and is already being discontinued.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.