Recent Posts

How to debug: unexpected T_VARIABLE PHP

An unexpected T_VARIABLE means that there's a $variable where there can't be one yet.

Missing semicolon

It most commonly indicates a missing semicolon in the previous line. Variable assignments following a statement are a good indicator where to look:

       
func1()
$var = 1 + 2;     # parse error in line +2

String concatenation

Another occurence is in string concatenation, where the concatenation operator . was forgotten:

                               
print "Here comes the value: "  $value;
Can happen for other expressions and operators of course.

Lists

Same for syntax lists, like in array populations, where the parser also indicates an expected comma , for example:

                                      
$var = array("1" => $val, $val2, $val3 $val4);
Or functions parameter lists:

                                
function myfunc($param1, $param2 $param3, $param4)
Equivalently do you see this with list or global statements, or when lacking a ; semicolon in a for loop.

Class declarations

This parser error also occurs in class declarations. You can only assign static constants, not expressions. Thus the parser complains about variables as assigned data:

class xyz {      
    var $value = $_GET["input"];
Unmatched } closing curly braces can in particular lead here. If a method is terminated too early (use proper indentation!), then a stray variable is commonly misplaced into the class declaration body.

Variables after identifiers

You can also never have a variable follow an identifier directly:

             
$this->myFunc$VAR();
Btw, this is a common example where you want to use variable variables instead.

Missing parens after language constructs

Hasty typing may lead to forgotten opening parenthesis for if and for and foreach statements:

       
foreach $array as $key) {
Solution: add the missing opening ( between statement and variable.

No comments:

Post a Comment