【问题标题】:Using an if statement on a php function在 php 函数上使用 if 语句
【发布时间】:2014-03-12 15:23:04
【问题描述】:

我正在尝试创建一个函数来测试三角形是否具有相等的边,然后打印答案,但我的函数不起作用。有任何想法吗 ?

 public function typeOfTriangle()
 {

    if ($this->lengthSideOne == $this->lengthSideTwo == $this->lengthBase)
    {echo 'the triangle is equal'}
 );
 }

【问题讨论】:

    标签: php function object if-statement


    【解决方案1】:

    您不能将== 操作串在一起。你需要使用AND(又名&&)。

    像这样:

    public function typeOfTriangle()
    {
        if ( $this->lengthSideOne == $this->lengthSideTwo && $this->lengthSideTwo == $this->lengthBase ) {
            echo 'the triangle is equal';
        }
    }
    

    【讨论】:

      【解决方案2】:

      公共函数 typeOfTriangle() {

      if ($this->lengthSideOne == $this->lengthSideTwo && $this->lengthSideOne == $this->lengthBase)
      {echo 'the triangle is equal';}
      

      ); }

      【讨论】:

        【解决方案3】:

        试试这个..

        public function typeOfTriangle()
         {
        
            if ($this->lengthSideOne == $this->lengthSideTwo && $this->lengthSideTwo == $this->lengthBase)
            {echo 'the triangle is equal'}
         );
         }
        

        【讨论】:

          【解决方案4】:
          public function typeOfTriangle()
           {
          
              if ( $this->lengthSideOne == $this->lengthSideTwo == $this->lengthBase )
              { echo 'the triangle is equal'; }
              // remove this );
           }
          

          【讨论】:

            【解决方案5】:

            错误是您的括号符号。

            public function typeOfTriangle() {
             if($this->lengthSideOne == $this->lengthSideTwo && $this->lengthSideTwo == $this->lengthBase) {
                 echo 'the triangle is equal';
              }
            }
            

            如果您使用括号,则语法为:

            if( ...condition... ) {
               ...do stuff...
            }
            

            无括号的条件是这样工作的

            if(...condition...)
               ...do stuff...
            

            更多信息在这里:http://www.php.net/manual/en/control-structures.if.php

            【讨论】:

              【解决方案6】:

              您需要将变量传递给函数。

              当您调用它时,请执行此操作。 (每个数字都是一边)

              typeOfTriangle(2,2,4)
              

              然后更改函数的开头以检索此数据并将其分配给 $this,如下所示。

                  public function typeOfTriangle($side1, $side2, $side3)
               {
              
                  if ($side1 == $side2 && $side2 == $side3) //this check side 1,2,3 are equal with 2 statements. 
                  {echo 'the triangle is equal';}
               }
              

              【讨论】:

              • 我没有投反对票,但我可以说你得到它们是因为你并不总是“需要”将参数传递给函数。我在函数声明中看到了 public 这个词,这意味着这可能是一个更大的类的一部分,其中 $this->sideX 正在设置。在这种情况下,您不需要将变量传递给函数来使用它们。
              • 我也不是投反对票的人,但是 xero 说您的原始答案仍然包括不正确的 if 语句(我看到您现在已经更改)并且您有语法错误');'
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2022-07-29
              • 2012-02-26
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-09-20
              • 1970-01-01
              相关资源
              最近更新 更多