Wiki source for CalcInfo

Show raw source

===This action allows you to put calculations (with variables or not) and write out the result.===

==Basic usage==
By typing:
1 day ={{calc 24*60*60}} seconds
you will obtain:
1 day =86400 seconds

==With variables usage==
By typing:
{{calc total=0}}
- red: {{calc q=2;total+=q;q}}
- blue: {{calc q=3;total+=q;q}}
- yellow: {{calc q=6;total+=q;q}}
- **total**: **{{calc total}}**
- **total+taxes**: **{{calc total+total*18.6/100.0}}**
You will obtain:
- red: 2
- blue: 3
- yellow: 6
- **total**: **11**
- **total+taxes**: **13.046**
- Several expressions separated by a semicolon can appear in a **calc** action,
- only those that aren't assignations (not containing the equal sign) are displayed.

- Secure
- The variables are keys in an associative array named calcvar (//i.e. calcvar['total']//)
- The function calls are forgotten.
- Powerful
- It makes your wiki page a spreadsheet.
- It allows displaying not hard-coded numerical values (such the number of seconds in a day), preventing errors...
- It allows modification of values in a page without recalculation by-hand of a global result (if you add an item //green: 12// in the list)

- Variables names are necessary alphabetics (see line 14).
- You can't (because of security) call functions (see line 28).
- A few operators are allowed (see line 16).

- You can easily add an operator (currently -*/+) by adding a char in the regexp line 16.
- You could authorize the call of specific functions (such ln, exp,...) by matching them (a little bit more difficult)
- You could define options in order to set the precision of the result by example.

Save this as actions/calc.php

*Author:Jean-fran├žois Delesse (
if (!function_exists('strtonum'))
function strtonum($str)
static $calcvar;
$pattern = '/([^;])+/';
foreach ($blocks[0] as $k=>$block)
foreach ($out as $k2=>$token)
if ($token[1]!="")
if ($token[2]!="")

if (!(strpos($tokstr,"=")===false))
$blockstr=eval("return ".$blockstr);
if ($affichage)
$outstr.=($outstr!=""?";":"").$blockstr ;
return $outstr;

if (is_array($vars))
print strtonum($vars['wikka_vars']);
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki