Skip to main content

CalcBuilder Tutorial

Calc Builder 6: the PHP code

The PHP Code screen is where the calculation happens, on the server, when the visitor presses the calculate button.

PHP Code editor

How it works

  • Every form field is available as a PHP variable named after its Variable: a field bill is $bill. Section fields are arrays.
  • You assign your own variables — $tip, $total, $schedule… — and any variable that exists at the end of the code can be printed in the result with ##name##.
  • Plain PHP works. The helper buttons above the editor (SUM, SUB, MUL, DIV, POW, MIN, MAX, NUMBER FORMAT…) insert convenience calls, and the drop-downs let you paste an input field or a detected variable.

Example (the Tip Calculator)

$tip_amount = $bill * $tip_percent / 100;
$total      = $bill + $tip_amount;
$per_person = ($people > 0) ? $total / $people : $total;

$tip_amount = number_format($tip_amount, 2);
$total      = number_format($total, 2);
$per_person = number_format($per_person, 2);

Format numbers for display after you finish calculating with them. You can also build HTML in a variable (a table, a list) and print the whole thing with one placeholder — that is how the Loan Calculator produces its amortization schedule.

The code runs with Joomla loaded, so use Joomla\CMS\Factory; and database queries are available if you need them.

...
Support/development

Perfect for small code changes or to correct any bug at your site

Buy now!
...
Support/development 10 hours

Get a bigger amount of hours for more complex tasks and get a 10% discount

Buy now!