1

Im trying to make a function that tests whether a triangle has equal sides and then print the answer but my function doesnt work. Any ideas ?

 public function typeOfTriangle()
 {

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

6 Answers 6

3

You can't string together == operations. You need to use AND (aka &&).

Like this:

public function typeOfTriangle()
{
    if ( $this->lengthSideOne == $this->lengthSideTwo && $this->lengthSideTwo == $this->lengthBase ) {
        echo 'the triangle is equal';
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

public function typeOfTriangle() {

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

); }

Comments

0

Try this..

public function typeOfTriangle()
 {

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

Comments

0
public function typeOfTriangle()
 {

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

Comments

0

the error is your parenthetical notation.

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

if your using brances the syntax is:

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

brace-less conditionals work like this

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

more info here: http://www.php.net/manual/en/control-structures.if.php

Comments

-2

You need to pass the variable to the function.

When you call it do this. (each number is a side)

typeOfTriangle(2,2,4)

Then change the start of your function to retrieve this data and assign it to $this like below.

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

2 Comments

i didn't down vote, but i can say your getting them because you dont' always "need to" pass params to a function. i see the word public in the function declaration, that means this is probably part of a larger class where $this->sideX is being set. in that context you dont need to pass variables to the function to use them.
I also wasn't the one to down vote, but as well what xero said your original answer still included the incorrect if statement (which I see you have changed now) and you have a syntax error with ');'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.