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
See if the line above the one reported in the error ends with ;
Do not use var
in PHP 5 onwards, prefer private
, protected
or public
.
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);
// ...
}
}
Put the rest of the code, you can’t help with just one line. The error depends on what you’re using.
– Maniero
this variable
$senha
was set before, certain?– Caio Felipe Pereira
And if you leave only $users = array('user1' => $password); without var?
– Giancarlo Abel Giulian
You’re in a class?
– Guilherme Nascimento
Must be missing one
;
at the end of the previous line– bfavaretto
As already said checks if one is missing; at the end of the previous line and removes the word var
– Adir Kuhn
What version of php is using?
– rray
In addition to removing this "var", you can cast to convert the password type: $users = array('user1' => (string) $password);
– Ivan Ferrer