2014-03-12 65 views
1

我试图做一个函数来测试一个三角形是否有相等的边,然后打印答案但我的函数不起作用。有任何想法吗 ?在php函数上使用if语句

public function typeOfTriangle() 
{ 

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

回答

3

您无法将字符串==操作。您需要使用AND(又名&&)。

像这样:

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

公共函数typeOfTriangle() {

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

); }

0

试试这个..

public function typeOfTriangle() 
{ 

    if ($this->lengthSideOne == $this->lengthSideTwo && $this->lengthSideTwo == $this->lengthBase) 
    {echo 'the triangle is equal'} 
); 
} 
-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';} 
} 
+0

我不不知道为什么人们在这个时候冷静下来rrect ... –

+3

我没有投票,但我可以说你得到他们,因为你不“总是”需要“传递参数到一个函数。我在函数声明中看到public一词,这意味着这可能是设置$ this-> sideX的较大类的一部分。在这种情况下,你不需要传递变量给函数来使用它们。 – xero

+0

我也不是一个投票,但也是什么xero说你的原始答案仍然包括不正确的if语句(我看你现在已经改变了),你有一个语法错误');' –

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

0
public function typeOfTriangle() 
{ 

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