在php函数上使用if语句

时间:2014-03-12 15:23:04

标签: php function object if-statement

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

 public function typeOfTriangle()
 {

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

6 个答案:

答案 0 :(得分:3)

您无法将==个操作串联起来。您需要使用AND(又名&&)。

像这样:

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

答案 1 :(得分:0)

public function typeOfTriangle()  {

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

);  }

答案 2 :(得分:0)

试试这个..

public function typeOfTriangle()
 {

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

答案 3 :(得分:0)

错误是您的括号表示法。

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

如果你使用brances的语法是:

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

无支撑条件就像这样工作

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

此处有更多信息:http://www.php.net/manual/en/control-structures.if.php

答案 4 :(得分:0)

public function typeOfTriangle()
 {

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

答案 5 :(得分:-2)

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

当你打电话时,这样做。 (每个数字都是一面)

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';}
 }