Home >Backend Development >PHP Tutorial >PHP exception Parse error: syntax error... error solution
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 no problem in using var
In fact, this is a very easy problem to solve . In my opinion, it seems familiar, haha, I recently learned JavaScript and 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
I was testing: When using a self-defined class object as a member of this class inside a class, something went wrong.
The address.inc code corresponding to the Address class:
The code is as follows:
<?php class Address { var $road; function Address(){} function setRoad($road){ $this->road = $road; } } ?>
Person class and its test code are person .php is as follows:
The 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(); ?>
Exception occurs in test output:
Parse error: syntax error , unexpected T_VAR in D:Apache2.2htdocsshirdrnpagep2pageUtil.inc on line 34
It’s because var is used to declare variables in the person.php code. This cannot be done in PHP. Just use the "$" symbol. It means that what follows this character is a PHP variable.