Home > Backend Development > PHP Tutorial > PHP exception Parse error: syntax error, unexpected error solution

PHP exception Parse error: syntax error, unexpected error solution

PHP中文网
Release: 2016-07-13 10:30:14
Original
7410 people have browsed it

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;
   }
}
?>
Copy after login

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();
?>
Copy after login

The test output is abnormal:

Parse error: syntax error, unexpected T_VAR in D:Apache2.2htdocsshirdrnpagep2pageUtil.inc on line 34

It is because var is used to declare variables in the person.php code. This cannot be done in PHP. Just use the "$" symbol to start. What follows this character is a PHP variable.

Related labels:
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template