PHP exception Parse error: syntax error, unexpected T_VAR error Solution

Source: Internet
Author: User
Tags parse error

In fact, this is a very easy problem to solve. In my opinion, I have been familiar with it. I recently learned how to use JavaScript to declare variables using var.

In fact, you do not need to use var declaration in PHP, but when a variable is a member variable of a class, there is no problem in using var.

When var is used externally, the following error occurs: Parse error: syntax error, unexpected T_VAR in..., for example, my error message:

Parse error: syntax error, unexpected T_VAR in D: \ Apache2.2 \ htdocs \ shirdrn \ page \ p2 \ pageUtil. inc on line 34

I'm testing: An error occurred while using a class object defined by myself as a member of the class within a class.

Address. inc code corresponding to the address class:

Copy codeThe Code is as follows:
<? Php
Class Address {
Var $ road;
Function Address (){}
Function setRoad ($ road ){
$ This-> road = $ road;
}
}
?>

The Person class and Its Test Code are as follows:

Copy codeThe Code is as follows:
<? Php
Require ("address. inc ");
Class Person {
Var $ name;
Var $ address;
Function Person (){
}
Function display (){
Echo "Name:". $ this-> name. "<BR> ";
Echo "Road:". $ this-> address-> road. "<BR> ";
}
}

Var $ p = new Person ();
$ P-> address = new Address ();
$ P-> address-> setRoad ("Chagnchun Road ");
$ P-> name = "Shirdrn ";
$ P-> display ();
?>

The test output is abnormal:

Parse error: syntax error, unexpected T_VAR in D: \ Apache2.2 \ htdocs \ shirdrn \ page \ p2 \ pageUtil. inc on line 34

This is because var is used in the person. php code to declare variables. This is not the case in PHP. As long as the "$" symbol is used to start, it indicates that the character is followed by a PHP variable.
Haha :-)

Supplemented by other netizens:

Solution: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or, two days ago, we started our php journey and made a very common website. As a result, our php version is 5.0, the server version is 4.0. I was busy for a day yesterday. This morning, I found a solution in an article. Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}'. If "public" exists, remove "public. No error occurs. If "public" is the definition variable, change "public" to "var ".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.