语法错误,意外的T_PRINT,期待T_STRING

时间:2012-06-29 17:46:34

标签: php

我收到了这个错误:

Parse error: syntax error, unexpected T_PRINT, expecting T_STRING in 
/homepages/26/d94605010/htdocs/lz/writecodeonline.com/php/index.php(190) 
: eval()'d code on line 7

示例:

   Class HelloWorld
    {   
      public $word;

      public function print()  
      {    
        echo $this->"hi";    
      }    
    }

    $word = new HelloWorld();
    $word = print();

3 个答案:

答案 0 :(得分:10)

print()是保留关键字。此外,这是无效的代码:

        echo $this->"hi";

您将收到此错误:

PHP Parse error:  syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting T_STRING or T_VARIABLE or '{' or '$'

你应该这样做:

Class HelloWorld
{   
  public $word = 'hi';

  public function printWord()  
  {    
    echo $this->word;    
  }    
}

$word = new HelloWorld();
$word = $word->printWord();

答案 1 :(得分:4)

print是PHP中的保留字。

您需要为您的方法指定其他名称。

答案 2 :(得分:0)

echo $this->"hi";替换为echo "hi";,致电$word->function();而不是$word = function();并将print重命名为其他