0

What I am trying to do is get an echo of the following php call and subtract 14.1% from the displayed number.

The code:

<?php echo $program->current_amount(); ?>

Can I add arithmetic functions to this in order to display the 14.1% deduction?

Justin
  • 3
  • 2
  • 7
    It's really not clear what you're asking. – Oliver Charlesworth Jul 07 '13 at 03:23
  • Are you just wanting to substract .141 from the value of `$program->current_amount()`? – Seth Jul 07 '13 at 03:27
  • Seth, yes I just want to subtract 14.1% from the value of $program->current_amount() so it is ECHOED on the page that way. I don't have to change the database at all. The display is for front end users, showing the 14.1% deduction from the number on the screen. It is only to show viewers the deduction. The database remains the same. – Justin Jul 07 '13 at 17:25

2 Answers2

1

You can perform expressions inside an echo statement, yes; just wrap it in a (), so:

<?php echo ($program->current_amount() - .141); ?>

It may not even be necessary to use (). Incidentally, if your environment supports short tags, you can simply do:

<?= $program->current_amount() - .141 ?>

Keep in mind, though, that that code won't actually remove 14.1% from your number--you would want to multiply by .859.

Way Spurr-Chen
  • 405
  • 2
  • 9
  • 1
    This will take 0.141 off of `current_amount`. It seems like OP wants 14.1% of `current_amount` removed from itself. – DACrosby Jul 07 '13 at 03:50
  • Thanks for the replies. I'm sorry if I was unclear. What I do want is to subtract 14.1% from the current_amount displayed on the page. It does not have to affect the database or anything even remotely close to that. I have a database display (the current_amount) that echos out and what I want to do is display that current_amount MINUS 14.1% – Justin Jul 07 '13 at 06:05
1

I think you're looking for a basic math operation in your output that has no effect on a database or anything else, correct?

If so, do something like the following:

<?php
 // Set values
  $current_amount = 100;
  $pcnt_off = 14.1;

 // Do the math
  $out = $current_amount - ($pcnt_off/100) * $current_amount;

 // Output
  echo $out . " is " . $pcnt_off . "% off of " . $current_amount;
?>

http://codepad.org/RqF8cuvN

More specifically to your case:

<?php echo $program->current_amount() - 0.141 * $program->current_amount(); ?>
DACrosby
  • 11,116
  • 3
  • 39
  • 51
  • DACrosby, Thanks for your reply. I put in the code above and it returns exactly what you stated, however, the $current_amount = 100; I'm not sure what this function is for? my $current_amount is pulled from a database and it reflects the current amount of money spent on a particular item. How do I set $current_amount to reflect the same as usual? Do I set it $current_amount = $current_amount; or something else? Thanks alot – Justin Jul 07 '13 at 23:58
  • @Justin My first code snip is a more generalized example of what's going on. Given a current amount of 100 and 14.1% off, output the values. You'll only need the second snip `$program->current_amount() - 0.141 * $program->current_amount();` which uses your variable from the database. – DACrosby Jul 08 '13 at 02:33
  • DaCrosby, Thanks again for the continued help. I plugged in the snip into the system but now it only returns a zero. I'm not really sure why it would be doing this if the arithmetic adds up. – Justin Jul 08 '13 at 03:26
  • Hm.. sounds like one of the variables isn't being set or caught properly. Maybe set `$program->current_amount()` to a variable first and force it to type `(float)`, then do the math? Try to reproduce the issue on a site like [CodePad](http://codepad.org/) and I'll take a look at your exact code, please. – DACrosby Jul 08 '13 at 04:33
  • DaCrosby, I've worked some of your code into a possible fix but before I consider this matter resolved, I wanted to follow up and A) Show what I did and B) ask for a follow up. I did: function current_amount( $current ) { $cut = $current * .141; $amount = $current - $cut; return $amount; } The only problem I'm having is that I'm showing too many decimals. If I do ($amount, 2) it only shows "3.7" but if I do ($amount, 3) it shows "3.695" for example. How can I set this decimal to show dollars and cents IE: 4.69 instead of 4.695. Do I need to use floor * 100 / ? Thanks! – Justin Jul 09 '13 at 18:48
  • [Here you go](http://stackoverflow.com/questions/4013950/how-do-i-print-currency-format-in-php) `return "$ ".number_format($amount, 2);` – DACrosby Jul 09 '13 at 19:34
  • DACrosby, thanks so much. You've solved all my issues with this troublesome code and I can't express how grateful I am. Thanks a lot! – Justin Jul 10 '13 at 07:02
  • No problem at all. Please mark the answer as correct if it is! Thanks! – DACrosby Jul 10 '13 at 15:12