1

im very new to PHP and im hoping someone here could help me out. i need to write a class, an when the below page echos it, it will show the answers.

<?php
include_once('Math.php');

$Math = new _Math();

echo $Math->calculate(2,3,"+")."<br />";
echo $Math->calculate(2,3,"-")."<br />";
echo $Math->calculate(2,3,"*")."<br />";
echo $Math->calculate(9,3,"/")."<br />";
echo $Math->calculate(9,0,"/")."<br />";
echo $Math->calculate("3",3,"+")."<br />";
echo $Math->calculate(2.5,3,"+")."<br />";
echo $Math->calculate(3,3,"test")."<br />";

I thought the code below would work, but im getting nothing but a blank screen.

<?php

class _Math {

 function calculate(2,3,"+"){
     $x = 2 + 3;
      return $x;
 }

 function calculate(2,3,"-"){
      $x = 2 - 3;
      return $x;
 }

 function calculate(2,3,"*"){
      $x = 2 * 3;
      return $x;
 }

 function calculate(9,3,"/"){
      $x = 9 / 3;
      return $x;
 }

 function calculate(9,0,"/"){
      $x = 9 / 0;
      return $x;
 }

 function calculate("3",3,"+"){
      $x = "3"+3;
      return $x;
 }

 function calculate(2.5,3,"+"){
      $x = 2.5+3;
      return $x;
 }

 function calculate(3,3,"test"){
      $x = 3 test 3;
      return $x;
 }

Im hoping someone can point me in the right direction. Hopefully im not that far off. Thanks in advance.

3
  • You have x number of calculate functions Commented Apr 1, 2014 at 8:13
  • @JakeN yes, and it smells like a horrible teacher. Commented Apr 1, 2014 at 8:13
  • Reading any PHP docs would have given you the answer. Commented Apr 1, 2014 at 8:16

3 Answers 3

4

Function arguments must be variables, not expressions, which is explained in the manual.

This is a partial implementation:

class _Math 
{
    function calculate($op1, $op2, $type)
    {
        switch ($type) {
            case '+': 
                return $op1 + $op2;

            case '-':
                return $op1 - $op2;
            // ... 
        }
    }
}

Inside the function you write a switch that will return the result based on the $type argument.

Sign up to request clarification or add additional context in comments.

Comments

1

You function should look like this

function calculate(num1, num2, operation){
    switch(operation){
         case '+': 
              return $num1 + $num2; 
         break;
         case '*':
              return $num1 * $num2; 
         break;

         // continue here :)

    }


}

You only need 1 function. and multiple function with the same name will throw an error in PHP.

Comments

0

This is not how you define functions. Im not even sure what youre trying to do.

The round brackets contain the parameters which you hand over to the function. So you call a function like that:

$Math->calculate(2,3,"+")

These last 3 things are the parameters. You have to define the function like this:

function calculate($x, $y, $operation){
    //your code
}

You cannot define multiple functions with the same name, so you have to check the operation and calculate it depending on the input. For example, +

function calculate($x, $y, $operation){
    if($operation === "+") {
        return $x + $y;
    }
}

Comments

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.