Search code examples
phpoopmathsubtraction

How to subtract with OOP PHP?


I am using oop php and I don't know how to subtract some equations that I have or how to do some arithmetic's.

For example:

class a {
public $account = "£1.00";
public $remove = ""
}

$p = new a;
$p->account = "£20.00";
$p->remove = "£5.00";

echo $p->account;
echo $p->remove;

So, as you can see in the example above is there a way I can subtract, add, multiple and divide. Also, is there a way to do percentages.

If someone can please help me, it would be much appreciated.

Many thanks!


Solution

  • First of all remove the Pound sign and it will be even better if you don't use strings but float values.

    $addition_result = $p->account + $p->remove; //add
    $subtraction_result = $p->account - $p->remove; //subtraction
    $division_result = $p->account / $p->remove; //division
    

    You can go ahead and echo those results.