rob.m
rob.m

Reputation: 10571

Supported php operand types?

I'm getting the following Fatal error: Unsupported operand types when I calculate a percentage using php operand, this is the code I'm using

       <p>
          <?php
             $baseprice = get_field_object('base_price');
             $basediscount = get_field_object('discount_applied'); 
          ?>
          Total price: 
           <?php 
             $division = $baseprice / $basediscount;
             $res = round($division * 100);
             echo $res; 
           ?>
       </p>

This is the link I am following for the code

Upvotes: 0

Views: 616

Answers (4)

Robert
Robert

Reputation: 20286

I've changed your code and simplify it It works. I've checked

     <?php
      $baseprice = 120.00;
      $basediscount = 10; //assuming it's 10%
      $discount = round($baseprice*$basediscount/100);
      $price_after_discount = $baseprice-$discount;
      //the other option to count price after discount with 1 line
      /*$price_after_discount = $baseprice-round($baseprice*$basediscount/100);*/ 
      echo "discount: $discount<br />";
      echo "price after discount $price_after_discount";
     ?>

With this code there are no errors and it counts discount very well Result for the code above is discount 12 price after discount 108

Notice:

You don't check $basediscount variable and if its 0 then it will be fatal error because you cannot divide by 0

Upvotes: 1

Roopendra
Roopendra

Reputation: 7762

Please correct your code:-

 $res = round($division * 100);

see manual how round work : http://php.net/manual/en/function.round.php

Check $baseprice and $basediscount data type. I am not sure it is integer or float.

I have generate one case for you if $baseprice or basediscount array than you will get this error. see here

http://codepad.org/BjxgN2lY

If it is int or float than it will surely work for you :- as in below demo.

http://codepad.org/JC9QxMio

Upvotes: 1

DRAJI
DRAJI

Reputation: 1869

    $res = round($division * 100);

before do this operation, do check if($division > 0 ) ie)

if($division > 0 )
 {
   $res = round($division * 100);
    echo $res;
  }

Upvotes: 0

h2ooooooo
h2ooooooo

Reputation: 39522

You seem to have forgotten your parenthesises to specify the parameters in the round function. You probably meant

$res = round($division * 100);

and not

$res = round$division * 100;

(which would make PHP think that you were trying to use $ as an operand, as opposed to the usual +, -, /, *, &, %, | etc.)

Upvotes: 2

Related Questions