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

PHP exception Parse error: syntax error, unexpected error solution

PHP中文网
PHP中文网Original
2016-07-13 10:30:147327browse

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

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.

Statement:
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