PHP Parse错误:语法错误,意外T_PUBLIC

Ash*_*dav 14 php

我在第3行的PHP代码中收到此错误,可能是什么错误?此代码取自frank在interactinet dot com上的 php手册用户注释

<?php

public function myMethod()
{
return 'test';
}

public function myOtherMethod()
{
return null;
}

if($val = $this->myMethod())
{
 // $val might be 1 instead of the expected 'test'
}

if( ($val = $this->myMethod()) )
{
// now $val should be 'test'
}

// or to check for false
if( !($val = $this->myMethod()) )
{
// this will not run since $val = 'test' and equates to true
}

// this is an easy way to assign default value only if a value is not returned:

if( !($val = $this->myOtherMethod()) )
{
$val = 'default'
}

?> 
Run Code Online (Sandbox Code Playgroud)

Div*_*com 41

public关键字仅用于类中的函数/变量声明.由于您没有使用类,因此需要将其从代码中删除.

  • 类中的公共函数和公开的函数有什么区别? (2认同)
  • 您也可以通过不关闭方括号&碰到下一个函数来遇到此错误...它将在该函数上引发此错误。那就是我所做的。别像我 (2认同)