【问题标题】:Other ways to check function's output?检查函数输出的其他方法?
【发布时间】: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 建议就可以了。

标签: php function class


【解决方案1】:
class Test{
public $flag;
private function func_one(){
    $this->flag = 1;
    return $this->flag;
}
public function display_func(){
    if ( !empty( $this->func_one() ) ){
        $result=$this->func_one();
        echo 'Flag is: '.$result;
    }
    }
}
$classTest = new Test();
$classTest->display_func();

【讨论】:

    【解决方案2】:

    将返回值存储在变量中并对其进行比较

    if ( !empty( $this->func_one() ) ){
        echo 'Flag is: ';
        $this->func_one();
    }
    

    应该是

    $val=$this->func_one();
    if (!empty($val)){
          echo "Flag is: $val";
    }
    

    同样从func_one() 中移除回声,只返回值。这允许您只调用一次该方法并根据返回的值做出决定。

    【讨论】:

      【解决方案3】:

      func_one() 中删除echo

      【讨论】:

      • 只是这样做导致“标志:”。
      【解决方案4】:

      您的条件语句:if ( !empty( $this->func_one() ) ) 调用func_one() 并且func_one() 的第二行是echo $this->flag;。这就是输出显示在 if 语句中的原因。

      【讨论】:

        【解决方案5】:

        您需要从第一个函数中删除 echo

        另外,在第二个函数中,连接从第一个函数返回的输出。

        因此,以下是更改:

        private function func_one(){
            $this->flag = 1;
            //echo $this->flag; -> remove this
            return $this->flag;
        }
        
        public function display_func(){
        
            if ( !empty( $this->func_one() ) ){
                echo 'Flag is: ' .$this->func_one(); // Also, observe the changes on this line.
            }
        

        【讨论】:

          【解决方案6】:

          您可以从第二个函数中删除 if 条件。见下面的代码。

          类测试{

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

          你的结果 = 标志是:1

          =========================================

          你也可以这样写代码

          类测试{

          public $flag;
          
          private function func_one() {
              $this->flag = 1;
              return $this->flag;
          }
          
          public function display_func() {
              echo 'Flag is: '.$this->func_one();
          }
          

          }

          $classTest = new Test(); $classTest->display_func();

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-01-22
            • 2011-09-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-02-11
            • 2014-12-14
            相关资源
            最近更新 更多