【发布时间】:2012-10-31 17:43:56
【问题描述】:
我在第 3 行的这个 PHP 代码中遇到了这个错误,可能是什么问题?此代码取自 php 手册user notes by frank at interactinet dot com
<?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'
}
?>
【问题讨论】:
-
看起来这应该是类定义的一部分。这不会按原样运行。
-
此外,代码展示了糟糕的编程,这是你不应该做的事情。为什么要使用代码?
标签: php