【发布时间】:2014-11-24 10:15:44
【问题描述】:
这里有一个明显的 PHP 新手提出的问题。
我有一个类,里面有两个函数。我正在尝试使用第二个函数检查第一个函数的输出(如下面的代码)。问题是输出 (1) 已经显示在 if 语句行中。结果如下:
1标志是:1
所以问题是,如果不将输出显示在 if 语句中,我该如何做到这一点?
class Test{
public $flag;
private function func_one(){
$this->flag = 1;
echo $this->flag;
return $this->flag;
}
public function display_func(){
if ( !empty( $this->func_one() ) ){
echo 'Flag is: ';
$this->func_one();
}
// if ( $this->func_one() === 1 ){
// echo 'Flag is: ';
// $this->func_one();
// }
//var_dump( $this->func_one() );
}
}
$classTest = new Test();
$classTest->display_func();
【问题讨论】:
-
删除
echo $this->flag;? -
... 并使用字符串连接来显示值:
echo 'Flag is: ' . $this->func_one(); -
这段代码有很多错误......你的函数同时分配、回声和返回......
-
只是删除回声导致“标志:”。添加@ThomasDavidPlat 建议就可以了。