2014-03-12 5 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

당신은 함수에 변수를 전달해야합니다.

전화 할 때이 작업을 수행하십시오. (각 숫자는 한 변임)

그런 다음이 데이터를 검색하여 아래의 $ 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

내가 돈 ' 이 사람이 협조 할 때 왜 사람들이 downvote하는지 모르겠다. rrect ... –

+3

나는 투표를하지 않았지만 '항상'함수에 params를 전달할 필요가 없기 때문에 그들을 얻는다고 말할 수 있습니다. 함수 선언에서 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); 
}