-3

I have this pieces of php here...

The value that gets written in HTML is this $16.67.

I am not sure what value type this is, not sure if it is a string or integar.

Regardless, I need to take this value and increase it by 3%.

I tried this but to no avail:

<?php echo $row['display_price' * 1.03]; ?>

If someone could help me out here this would be great.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Robert Henry
  • 43
  • 1
  • 9

2 Answers2

2

You are trying to multiple display_price string and use it as array key and you should just increase the value that the variable holds.

echo $row['display_price'] * 1.03;

If variable holds the dollar sign too (it shouldn't!) use this regex to get the value and multiply it. Example:

echo "$" . ( floatval( preg_replace( "/[^-0-9\.]/", "", $var ) ) * 1.03 );
Community
  • 1
  • 1
nass
  • 382
  • 1
  • 6
  • 19
  • What if `$row['display_price']` contains a dollar sign ? Which I believe it does. – Daan Apr 04 '16 at 14:28
  • @Daan replace it with `str_replace()` :) – node_modules Apr 04 '16 at 14:29
  • 1
    @C0dekid.php I wouldn't go for `str_replace()`. Anyways that should be in the answer. – Daan Apr 04 '16 at 14:30
  • @Daan Added to answer. – nass Apr 04 '16 at 14:36
  • The value that we are working with is a string and is proceeded by a $Dollar Sign. As such this piece of php does not know how to multiply a string and returns a null 0 value. Can someone tell me how to convert this value to an integer and remove the $ sign and then we can multiply. All help appreciated. – Robert Henry Apr 04 '16 at 14:37
  • @RobertHenry I've edited the answer. Use second line of code. – nass Apr 04 '16 at 14:41
  • Thanks All!!!! So this is my returned Value = $17.1701, How do I shave off the last two decimals so that it only has two? – Robert Henry Apr 04 '16 at 14:52
  • @RobertHenry Use [`round()`](http://php.net/manual/en/function.round.php) function with `precision` set to `2`. – nass Apr 04 '16 at 14:58
1

You need to get the float value of your variable. And then, you increase it.

$increasedValue = floatval($row['display_price']) * 1.03;
echo '$' . $increasedValue;

This will return what you want, with the dollar sign at the beginning.

Rarasen
  • 64
  • 3
  • It won't work! `floatval()` for non-numeric leftmost characters returns `0`! Example: http://ideone.com/6z4k5M – nass Apr 04 '16 at 14:40