How to solve php exception parse error: syntax error, unexpected t_var error

WBOY
Release: 2016-07-25 09:12:46
Original
1690 people have browsed it

In PHP, variables do not need to be declared with var, but when a variable is used as a member variable of a class, there is still no problem using var.

Actually, this is a very easy problem to solve. In my opinion, it seems familiar, haha, I recently learned javascript but learned to use var to declare variables. In fact, there is no need to use var declaration in PHP, but when a variable is used as a member variable of a class, there is still no problem in using var. When using var externally, an 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.2htdocsshirdrnpagep2pageutil.inc on line 34

Test: An error occurred when using a self-defined class object as a member of the class inside a class. 1. The address.inc code corresponding to the address class:

  1. class address {
  2. var $road;
  3. function address(){}
  4. function setroad($road){
  5. $this->road = $road;
  6. }
  7. }
  8. ?>
Copy code

2, person class and test code are person.php:

  1. require("address.inc");
  2. class person {
  3. var $name;
  4. var $address;
  5. function person(){
  6. }
  7. function display(){
  8. echo "name : ".$this->name."
    ";
  9. echo "road : ".$this->address->road."
    ";
  10. }
  11. }
  12. var $p = new person();
  13. $p->address = new address();
  14. $p->address->setroad("chagnchun road");
  15. $p->name = "shirdrn" ;
  16. $p->display();
  17. ?>
Copy code

The test output is abnormal: parse error: syntax error, unexpected t_var in d:apache2.2htdocsshirdrnpagep2pageutil.inc on line 34 Because var is used to declare variables in the person.php code, this cannot be done in PHP. As long as the "$" symbol is used at the beginning, it means that what follows this character is a PHP variable.

Supplementary methods: Problem solved: syntax error, unexpected t_string, expecting t_old_function or t_function or t_var or I started my own PHP journey two days ago and made a very ordinary website. It turns out that our PHP is version 5.0 and the server is version 4.0. I am so confused.

Solution: parse error: syntax error, unexpected t_string, expecting t_old_function or t_function or t_var or '}', if there is "public", remove "public". There will be no error. If "public" is a defined variable, change "public" to "var".



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template