在php函数上使用if语句
我试着做一个函数,测试三角形的边是否相等,然后打印答案,但我的函数不起作用。有什么想法吗在php函数上使用if语句,php,function,object,if-statement,Php,Function,Object,If Statement,我试着做一个函数,测试三角形的边是否相等,然后打印答案,但我的函数不起作用。有什么想法吗 public function typeOfTriangle() { if ($this->lengthSideOne == $this->lengthSideTwo == $this->lengthBase) {echo 'the triangle is equal'} ); } 不能将==操作串在一起。您需要使用和(又称&&) 像这样: public fun
public function typeOfTriangle()
{
if ($this->lengthSideOne == $this->lengthSideTwo == $this->lengthBase)
{echo 'the triangle is equal'}
);
}
不能将
==
操作串在一起。您需要使用和
(又称&&
)
像这样:
public function typeOfTriangle()
{
if ( $this->lengthSideOne == $this->lengthSideTwo && $this->lengthSideTwo == $this->lengthBase ) {
echo 'the triangle is equal';
}
}
公共函数typeOfTriangle() { )); }试试这个
public function typeOfTriangle()
{
if ($this->lengthSideOne == $this->lengthSideTwo && $this->lengthSideTwo == $this->lengthBase)
{echo 'the triangle is equal'}
);
}
您需要将变量传递给函数 当你叫它的时候,做这个。(每个数字都是一个边) 然后更改函数的开头以检索此数据并将其分配给$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 function typeOfTriangle() {
if($this->lengthSideOne == $this->lengthSideTwo && $this->lengthSideTwo == $this->lengthBase) {
echo 'the triangle is equal';
}
}
如果使用分支,则语法为:
if( ...condition... ) {
...do stuff...
}
无括号条件语句的工作方式如下
if(...condition...)
...do stuff...
这里有更多信息:我没有否决投票,但我可以说你得到了它们,因为你并不总是“需要”将参数传递给函数。我在函数声明中看到了public这个词,这意味着它可能是一个更大类的一部分,在这个类中设置了$this->sideX。在这种情况下,您不需要将变量传递给函数来使用它们。我也不是投反对票的人,但是xero说您的原始答案仍然包含错误的if语句(我看到您现在已经更改了),并且您的“;”有语法错误
if( ...condition... ) {
...do stuff...
}
if(...condition...)
...do stuff...
public function typeOfTriangle()
{
if ( $this->lengthSideOne == $this->lengthSideTwo == $this->lengthBase )
{ echo 'the triangle is equal'; }
// remove this );
}