Recent Posts

Showing posts with label T_VARIABLE. Show all posts
Showing posts with label T_VARIABLE. Show all 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.

How to solve: Parse error: syntax error, unexpected T_VARIABLE

Possible scenario
I can't seem to find where my code has went wrong. Here is my full error:
Parse error: syntax error, unexpected T_VARIABLE on line x
what i am trying
$sql = 'SELECT * FROM dealer WHERE id="'$id.'"';
Answer
Parse error: A problem with the syntax of your program, such as leaving a semicolon off of the end of a statement or in like in above case missing . operator . The interpreter stops running your program when it encounters a parse error.
In simple word this is a syntax error, meaning that there is something in your code stopping it from being parsed correctly and therefore run.
What you should do is check carefully at the lines around where the error is for any simple mistakes
That error message means that in line x of the file, the PHP interpreter was expecting to see an open parenthesis but instead, it encountered something called T_VARIABLE. That T_VARIABLE thing is called a token. It's the PHP interpreter's way of expressing different fundamental parts of programs. When the interpreter reads in a program, it translates what you've written into a list of tokens. Wherever you put a variable in your program, there is aT_VARIABLE token in the interpreter's list.
so make sure you enable at least E_PARSE in your php.ini. Parse errors should not exist in production scripts.
i always recommended to while coding
error_reporting(E_ALL);
Also a good idea to use IDE which will let you know parse error while typing. You can sue
  1. NetBeans (fine peace of beauty, free)(imho its best)
  2. PhpStorm (Uncle Gordon love this :P , paid)
  3. Eclipse (beauty and the beast free)