user3411363
user3411363

Reputation: 25

Using an if statement on a php function

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

Upvotes: 1

Views: 75

Answers (6)

Matt The Ninja
Matt The Ninja

Reputation: 2729

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

Upvotes: -2

xero
xero

Reputation: 4319

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

Upvotes: 0

Beau
Beau

Reputation: 73

public function typeOfTriangle()
 {

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

Upvotes: 0

martskins
martskins

Reputation: 2940

Try this..

public function typeOfTriangle()
 {

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

Upvotes: 0

Mazeltov
Mazeltov

Reputation: 551

public function typeOfTriangle() {

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

); }

Upvotes: 0

soapergem
soapergem

Reputation: 9989

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

Upvotes: 3

Related Questions